Count pairs with given sum | Practice | GeeksforGeeks

Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K.

Example 1:
Input:
N = 4, K = 6
arr[] = {1, 5, 7, 1}
Output: 2
Explanation: 
arr[0] + arr[1] = 1 + 5 = 6 
and arr[1] + arr[3] = 5 + 1 = 6.


Example 2:
Input:
N = 4, K = 2
arr[] = {1, 1, 1, 1}
Output: 6
Explanation: 
Each 1 will produce sum 2 with any 1.

Your Task:
You don't need to read input or print anything. Your task is to complete the function getPairsCount() which takes arr[], n and k as input parameters and returns the number of pairs that have sum K.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 <= N <= 105
1 <= K <= 108
1 <= Arr[i] <= 106
 
0
vermaankush99110 hours ago
// very simple code
class Solution{   public:   int getPairsCount(int arr[], int n, int k) {       int count=0;       unordered_map<int,int>map;       for(int i=0; i<n; i++){           if(map.find(k-arr[i])!=map.end())           count+=map[k-arr[i]];           map[arr[i]]++;                  }       return count;   }}; 
0
dhanakdeepak421 day ago
class Solution {   int getPairsCount(int[] a, int n, int sum) {       // code here       int c=0;    HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); for(int i=0;i<n;i++) {  if(!hm.containsKey(a[i]))  {    hm.put(a[i],0);  }      hm.put(a[i],hm.get(a[i])+1); } for(int i=0;i<n;i++) {  if(hm.get(sum-a[i])!=null)  {   c=c+hm.get(sum-a[i]);  }  if(sum-a[i]==a[i])  {   c--;  } }      return c/2;   }} 
0
aakasshuit1 day ago
Easy Java Solution:
 
        int count = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        
        for(int i=0;i<n;i++){
            if(map.containsKey(k-arr[i])){
                count+=map.get(k-arr[i]);
            }
            map.put(arr[i],map.getOrDefault(arr[i],0)+1);
        }
        return count;
    }
0
praveenanan44621 day ago
class Solution{   public:   int getPairsCount(int arr[], int n, int k) {       // code here       unordered_map<int, int >mapp;              for(int i= 0;i<n;i++) {           mapp[arr[i]]++;       }       int ans= 0;       for(int i= 0;i<n;i++) {           mapp[arr[i]]--;           if(mapp.find(k-arr[i])!=mapp.end()) {               ans +=mapp[k-arr[i]];// mapp[k-arr[i]] will give the //frequency of the particular element           }       }       return ans;   }};
0
afredhussain693 days ago
 
Java Soultion :
 
int getPairsCount(int[] arr, int n, int k) {       int pairCount=0;       HashMap<Integer,Integer> pairs=new HashMap<>();       for(int i=0;i<n;i++){           if(pairs.containsKey(k-arr[i])){               pairCount=pairCount+pairs.get(k-arr[i]);           }           pairs.put(arr[i],pairs.getOrDefault(arr[i],0)+1);       }       return pairCount;   }
0
dkbhargava1013 days ago
Easy ad fast c++ solution in O(n) time and O(n) space takes 0.7/1.82 sec
code:
class Solution{   public:   int getPairsCount(int arr[], int n, int k) {       int ans=0;       map<int,int> mp;       for(int i=0;i<n;i++){           mp[arr[i]]++;       }       for(int i=0;i<n;i++){           mp[arr[i]]--;           if(k-arr[i]>0){           ans=ans+mp[k-arr[i]];}       }       return ans;   }};
0
badgujarsachin833 days ago
 int getPairsCount(int arr[], int n, int k) {
        unordered_map<int,int> mp;
        int c=0;
        for(int i=0;i<n;i++){
            if(mp.find(k-arr[i])!=mp.end()){
                c+=mp[k-arr[i]];
            }
            mp[arr[i]]++;
        }
        return c;
        
    }
0
hoodninja5 days ago
Easy C++ solution 
    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 count = 0;
        for(int i=0;i<n;i++)
        {
            count = count+u[k-arr[i]];
            if(k-arr[i] == arr[i])
                count--;
        }
        return count/2;
    }
    
+1
kumarr32035 days ago
  //c++ solution
   
    
     
       unordered_map<int ,int >m;
         int cnt=0;
         for(int i=0;i<n;i++)
         {
             if(m.find(k-a[i])!=m.end())
             {
                 cnt=cnt+m[k-a[i]];
             }
             m[a[i]]++;
             
         }
        return cnt;
        
-1
thebadguy6 days ago
python solution
 
def getPairsCount(self, arr, n, k):
        # code here
        count=0
        seen={}      
        for i in arr:
            c=k-i
            if c in seen:
                count+=seen[c]
            seen[i]=seen.get(i,0)+1
                
        return count
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.