Count distinct pairs with difference k | Practice | GeeksforGeeks

Given an integer array and a non-negative integer k, count all distinct pairs with difference equal to k, i.e., A[ i ] - A[ j ] = k
 
Example 1:
Input: array = {1, 5, 4, 1, 2}, k = 0
Output: 1
Explanation: There is only one pair (1, 1)
whose difference equal to 0.

Example 2;
Input: array = {1, 5, 3}, k = 2
Output: 2
Explanation: There are two pairs (5, 3) and 
(1, 3) whose difference equal to 2.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function TotalPairs() which takes array and k as input parameter and returns count of all distinct pairs.
 
Expected Time Complexity: O(Max(Ai))
Expected Space Complexity: O(Max(Ai))
 
Constraints:
0 <= length of array, k <= 100000
1 <= elements of array <= 100000
0
mayank20212 days ago
C++ : 1.22/2.51int TotalPairs(vector<int>nums, int k){        int count=0;        unordered_map<int, int>mp;    for(int i=0; i<nums.size(); i++)         mp[nums[i]]++;               for(int i=0; i<nums.size(); i++)       {           if(!k && mp[nums[i]]>1) count++;                      else if(k && mp[nums[i]] && mp[nums[i]+k]) count++;           if(k && mp[nums[i]] && mp[nums[i]-k]) count++;                      mp.erase(nums[i]);                  }              return count;    }
+2
shivanshpandey1573 weeks ago
unordered_map<int,int>mp;    int count=0;    for(int i=0;i<arr.size();i++)    {        mp[arr[i]]++;    }    for(auto x:mp)    {        if(mp.find(x.first-k)!=mp.end())        {            if((x.first-k)==x.first)            {                if(x.second>1)                count++;            }            else            count++;        }    }    return count;
0
shadabarshad162 months ago
time-o(n)
space-o(n)class Solution{   public int TotalPairs(int[] a, int k)   {       int n = a.length;       int c=0;       HashMap<Integer,Integer>map=new HashMap<>();        for(int i=0;i<n;i++)           {               if(map.containsKey(a[i]))               {                   map.put(a[i],map.get(a[i])+1);               }               else               {                   map.put(a[i],1);               }           }           for(int i=0;i<n;i++)           {               if(map.containsKey(a[i]-k))               {                   if(a[i] == a[i]-k && map.get(a[i]-k)==1)                   {                       continue;                   }                   else{                      c++;                      map.remove(a[i]-k);                   }               }           }
       return c;   }}
0
akkeshri140420012 months ago
int TotalPairs(vector<int>nums, int k){
    // Code here
    map<int,int>m;
    int count=0;
    for(int i=0;i<nums.size();i++){
        m[nums[i]]++;
    }
    for(auto x:m){
        if(k==0){
            if(x.second>1){
                count++;
            }
        }
        else{
            if(m.find(x.first-k)!=m.end()){
                count++;
            }
        }
    }
    return count;
}
0
codertheta12 months ago
TC O(nlogn) SC O(1)
 Arrays.sort(nums);
        int i = 0, j = 1,ans = 0;
        while(i < nums.length && j < nums.length){
            
            if(i > 0 && i < nums.length && nums[i - 1] == nums[i]){i++; continue;}
            
            int diff = nums[j] - nums[i];
            
            if(i == j){
                if(target < 0)
                    i++;
                else
                    j++;
                continue;
            }
            
            if(diff == target){
                ans++;
                i++;
                j++;
            }
            else if(diff < target) j++;
            else
                i++;
        }
        return ans;
0
independentanup2 months ago
pls check my mistake
 
int TotalPairs(vector<int>nums, int k){    // Code here    sort(nums.begin(),nums.end());    int count=0;    unordered_map<int,int>mp;    for(int i=0;i<nums.size();i++){        mp[nums[i]]++;    }    for(auto i=mp.begin();i!=mp.end();i++){      auto it =mp.find((i->first)+k);      if(it!=mp.end()){          count=count+(i->second) * (it->second);      }       }      return count;}
 
+2
avinav26113 months ago
Easiest C++ Solution (working!)
 
 
The idea is to distinguish all the duplicates elements and then count the pair-difference......and sorted in descending order so that all the sums of smaller elements are taken into consideration.
0
rathoredivya1503 months ago
class sloution{public:int TotalPairs(vector<int>nums, int k){    unordered_map<int,int>mp;    int count=0;    typedef pair<int, int> pairs;
set<pairs>s;    for(int i=0;i<nums.size();i++)    {        mp[nums[i]]++;    }                   if(k==0){             for(int i=0;i<nums.size();i++)         {        if(mp[nums[i]]>1)                {            count++;            mp.erase(nums[i]);        }                    }}        else        {            for(int i=0;i<nums.size();i++)           {                       if(mp.find(nums[i]+k)!=mp.end())        {            count++;           pairs x=make_pair(nums[i]+k,nums[i]);           s.insert(x);        }    }            count=s.size();        }return count;    // Code here}};
 
 
0
mhbhuiyan10023
This comment was deleted.
0
mhbhuiyan10023
This comment was deleted.
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.