Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
count pairs with given sum hashing
//Initial template for C++ #include <bits/stdc++.h> using namespace std; // } Driver Code Ends //User function template for C++ class Solution{ public: int getPairsCount(int arr[], int n, int k) { // code here unordered_map<int,int>u; for(int i=0;i<n;i++) { u[arr[i]]++; } int c=0; for(int i=0;i<n;i++) { if(u.count(k-arr[i])) { u[arr[i]]--; c+=u[k-arr[i]]; } } return c; } }; // { Driver Code Starts. int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } Solution ob; auto ans = ob.getPairsCount(arr, n, k); cout << ans << "\n"; } return 0; } // } Driver Code Ends
run
|
edit
|
history
|
help
0
Sequence point and array
mytemp
char strcpy
How to make stupid to my friend?
ExceptionHandling3
Meeting_Leandro
Procesos E
MyList
check Prime
can be zero