- Prepare
- Algorithms
- Search
- Pairs
- Discussions
Pairs
Pairs
+ 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 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 comments for(int i = 0; i < arr.size(); i++) { if(container[arr[i] + k]) { count++; } container[arr[i]]--; } return count;
}
+ 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 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.
Sort 1141 Discussions, By:
Please Login in order to post a comment