We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Search
  4. Pairs
  5. Discussions

Pairs

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 1141 Discussions, By:

recency

Please Login in order to post a comment

  • 2001640100149A
    15 hours ago+ 0 comments

    Simple Java Solution

    public static int pairs(int k, List arr) { int count = 0; HashMap map = new HashMap<>(); for(int i : arr) { map.put(i, map.getOrDefault(i, 1)+1); } for(Map.Entry e: map.entrySet()) { int sum = e.getKey() - k; if(map.containsKey(sum)) { count++; } } return count; }

    }

    }
    

    0|
    Permalink
  • abusamak1948
    3 days ago+ 0 comments

    int pairs(int k, vector arr) { unordered_map container; for(int i = 0; i < arr.size(); i++) { container[arr[i]]++; } int count = 0;

    0|
    Permalink
  • abusamak1948
    3 days ago+ 0 comments
    for(int i = 0; i < arr.size(); i++) { 
        if(container[arr[i] + k]) { 
            count++; 
        } 
        container[arr[i]]--; 
    }
    
    return count;
    

    }

    0|
    Permalink
  • abusamak1948
    3 days ago+ 0 comments

    int pairs(int k, vector arr) { unordered_map container; for(int i = 0; i < arr.size(); i++) { container[arr[i]]++; } int count = 0;

    for(int i = 0; i < arr.size(); i++) { 
        if(container[arr[i] + k]) { 
            count++; 
        } 
        container[arr[i]]--; 
    }
    
    return count;
    

    }

    0|
    Permalink
  • karlshady1990
    3 weeks ago+ 0 comments

    I really enjoyed this challenge! I found that the solution provided in the editorial was the most efficient way to solve it. However, I did try a brute force approach initially, which quickly became inefficient for larger arrays. It's great to see how algorithms can be optimized for performance in real-world scenarios.

    In fact, I'm currently working on developing a https://pokieslab.com/real-money-pokies/ for a real money online pokies game, and I think this algorithm could be useful in identifying pairs of matching symbols on the reels. I plan on using this algorithm as a basis for optimizing the search for matching symbols on the reels, as it is crucial for the player's chances of winning.

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy