Pairs Two Pointer Solution Time : O(nlogn) Space : O(1) 12345678910111213int pairs(int k, vector<int> A) { sort(begin(A), end(A)); int res = 0, n = A.size(), l = 0, r = 0; while(r < n) { while(r < n and A[l]*1ll + k > A[r]) r++; if(A[l]*1ll + k == A[r]) { res++; l++, r++; } else l++; } return res;} Hash Table Solution Time : O(n) Space : O(n) 1234567891011int pairs(long k, vector<int> A) { unordered_set<long> us; int res = 0; for(auto& a : A) { if(us.count(a-k)) res++; if(us.count(a+k)) res++; us.insert(a); } return res;}