Count pairs in array divisible by K | Practice | GeeksforGeeks

Given an array A[] and positive integer K, the task is to count total number of pairs in the array whose sum is divisible by K.
Example 1:
Input : 
A[] = {2, 2, 1, 7, 5, 3}, K = 4
Output : 5
Explanation : 
There are five pairs possible whose sum
is divisible by '4' i.e., (2, 2), 
(1, 7), (7, 5), (1, 3) and (5, 3)
Example 2:
Input : 
A[] = {5, 9, 36, 74, 52, 31, 42}, K = 3
Output : 7 
Explanation : 
There are seven pairs whose sum is divisible by 3, 
i.e, (9, 36), (9,42), (74, 52), (36, 42), (74, 31), 
(31, 5) and (5, 52).

0
shubhamkhuntia3 weeks ago
class Solution
{
    public static long countKdivPairs(int arr[], int n, int k)
    {
        HashMap<Integer, Integer> map = new HashMap<>();
		for(int val:arr){
			int rem = val%k;
			if(rem<0){
			    rem+=k;
			}
			map.put(rem,map.getOrDefault(rem,0)+1);
		}
		long ans = 0;
		for(int val: arr){
			int rem = val%k; 
			if(rem<0){
			    rem+=k;
			}
			if(rem==0){
					long pair = (long)map.get(rem);
					ans += pair*((pair-1)/2);
			} else if(2*rem == k){
				
					long pair = (long)map.get(rem);
					ans += pair*((pair-1)/2);
				
			} else {
				
					long pair = (long)map.get(rem);
					long pair2 = (long)map.get(k-rem);
					ans += pair*pair2;
				
			}
			
		}
		return ans;
    }
}
0
ankitpahwa4 weeks ago
long long countKdivPairs(int A[], int n, int k)
    {
        //code here
        if(k==1)
            return n*(n-1)/2;
            
        unordered_map<int,int>m;
        vector<int>remainders(n,0);
        for(int i=0; i<n; i++){
            remainders[i] = A[i]%k;
        }
        long long ans = 0;
        for(int i=0; i<n; i++){
            int myself = remainders[i];
            int target1 = k-remainders[i];
            int target2 = -1*remainders[i];
            if(m.count(target1)==1 && m[target1]>0) ans += m[target1];
            if(m.count(target2)==1 && m[target2]>0) ans += m[target2];
            m[myself]++;
        }
        return ans;
    }
0
jayesh294 weeks ago
Easy 5 line Java Code :)
    public static long countKdivPairs(int arr[], int n, int k){
        HashMap<Integer,Integer> map = new HashMap<>();
        long count = 0;
        for(int i=0;i<n;i++){
            if(map.containsKey((k-(arr[i]%k))%k))    count+=(map.get((k-(arr[i]%k))%k));
            map.put(arr[i]%k,map.getOrDefault((arr[i]%k),0)+1);
        }
        return count;
    }
0
zacharibarnes1 month ago
Why doesnt my code work on testcase 71 and 72?
Javascript:

    countKdivPairs(arr,n, k){

        const remainderArr = new Array(k).fill(0);
        for(let x of arr){
            remainderArr[x%k]++;
        }
        let totalCount = (remainderArr[0] * (remainderArr[0]-1))/2;

        for(let i=1; i<=k/2 && i !==(k-i); i++){
            totalCount += remainderArr[k-i] * remainderArr[i];
        }
        if(k%2 == 0)
        totalCount += (remainderArr[k/2] * (remainderArr[k/2]-1))/2;
        // console.log(remainderArr);
        return totalCount;
    }
0
imranwahid2 months ago
C++ solution using hash
0
amruthack11012 months ago
JAVA:
class Solution
{
    public static int countKdivPairs(int arr[], int n, int k)
    {
        HashMap<Integer,Integer> map=new HashMap<>();
        int ans=0;
        for(int i=0;i<n;i++){ 
            if(map.containsKey((k-(arr[i]%k))%k))
                ans+=map.get((k-(arr[i]%k))%k);
               
            int cV=arr[i]%k;
            map.put(cV,map.getOrDefault(cV,0)+1  );
        }
        //System.out.print(map);
        return ans;
    }
}
+1
patelneer4032 months ago
//perfect sol
int countKdivPairs(int A[], int n, int K)   {      unordered_map<int,int>map;      int i,count=0;            if(K==1)      return n*(n-1)/2;            for(i=0;i<n;i++)      {                    map[A[i]%K]++;      }      for(i=1;i<=K/2;i++)      {          if(i!=K-i)          count+= map[i]*map[K-i];      }      if(K%2==0)      count+= map[K/2]*(map[K/2]-1)/2;            count+= map[0]*(map[0]-1)/2;            return count;          }
+1
sknwd88643 months ago
Time Complexity : O(N)
Space Complexity : O(K)
int countKdivPairs(int arr[], int n, int k)
    {
        unordered_map<int, int> mp;
        
        int count = 0;
        
        for(int i = 0; i < n; i++)
        {
            int rem = arr[i] % k;
            
            int curr = (k - rem) % k ;
            
            auto it = mp.find(curr);
            
            if(it != mp.end())
            {
                count += mp[curr];
            }
            
            mp[rem]++;
        }
        
        return count;
    }
0
nav575kaur3 months ago
int countKdivPairs(int A[], int n, int K)
    {
        //code here
        vector<int> freq(K,0);
        int ans = 0;
        for(int i=0; i<n; i++){
            //find complement
            int comp = (K-(A[i]%K))%K;
            ans = ans+freq[comp];
            freq[A[i]%K]++;
        }
        return ans;
    }
0
deepakchill77333 months ago
 int countKdivPairs(int A[], int n, int K)
    {unordered_map<int,int> mp;
    
    int f=0;
    for(int i=0;i<n;i++){
        if(A[i]%K!=0){
        int g=K-(A[i]%K);
        if(mp[g]>=1){f+=mp[g];}}
        else{if(mp[0]>=1){f+=mp[0];}}
        mp[A[i]%K]++;
    }
    return f;
        //code here
    }
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.