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.
- Prepare
- Algorithms
- Search
- Pairs
- Discussions
Pairs
Pairs
Sort by
recency
|
1243 Discussions
|
Please Login in order to post a comment
C#
Python 3:
from collections import Counter
counter = Counter(arr) num_pairs = 0
for i in arr: if i - k in counter: num_pairs += 1 # Note: integers in arr are distinct
return num_pairs
Simple C++ sol using set | O(n)
`c++
int pairs(int k, vector arr) { unordered_set st; int n = arr.size(), c = 0; for (int i=0; i
`
Nice and easy this time! First time I post a solution here, because I'm stunned it worked at the very first time, wihtout any corrections (rare case;)
Sort + caterpillar method.
Java solution:
Python3 Optimal Solution:
def pairs(k, arr):