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
|
1244 Discussions
|
Please Login in order to post a comment
include
using namespace std; int test(int n, long long k, vector a) { int dem = 0; unordered_sets(a.begin(), a.end()); for(int i = 0; i < n; i++) { if(a[i] > k){ long long tam = a[i] - k; if(s.find(tam) != s.end()) { dem++; } } } return dem; } int main() { int n; long long k; cin >> n >> k; vector a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } cout << test(n, k, a); return 0; }
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: