Array Pair Sum Divisibility Problem | Practice | GeeksforGeeks

Given an array of integers and a number k, write a function that returns true if given array can be divided into pairs such that sum of every pair is divisible by k.
 
Example 1 :
Input : arr = [9, 5, 7, 3], k = 6
Output: True
Explanation: {(9, 3), (5, 7)} is a 
possible solution. 9 + 3 = 12 is divisible
by 6 and 7 + 5 = 12 is also divisible by 6.


Example 2:
Input : arr = [2, 4, 1, 3], k = 4
Output: False
Explanation: There is no possible solution.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function canPair() which takes array and k as input parameter and returns true if array can be divided into pairs such that sum of every pair is divisible by k otherwise returns false.
 
Expected Time Complexity: O(n)
Expected Space Complexity : O(n)
 
Constraints:
1 <= length of array <= 100000
1 <= elements of array <= 100000
1 <= k <= 100000
0
akshayjadhav34 days ago
c++ with hash map
 
bool canPair(vector<int> nums, int k) {       // Code here.       unordered_map<int,int>s;      if(nums.size()%2!=0)      {          return false;      }                               for(int i=0;i<nums.size();i++)       {                      s[nums[i]%k]++;                             }                     for(auto x : s)       {                      if(x.first==0)           {                              if(x.second%2!=0)               {                   return false;               }                          }                      else            {                              int a=x.first;               auto it=s.find(k-a);               if(it==s.end())               {                   return false;               }               else               {                   if((*it).second!=x.second)                   {                       return false;                   }                                  }                                                        }                                                              }                                           return true;          }
 
 
 
0
gulrezalam5 days ago
Java Solution



Total Time Taken:
0.65/2.3





class Solution {
  
        public boolean canPair(int[] nums, int k) {
        if (nums.length % 2 != 0)
            return false;
        else if (k == 1 || k == 0)
            return true;
            
        HashMap<Integer, Integer> rem = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int remainder = nums[i] % k;
            rem.put(remainder, rem.getOrDefault(remainder, 0) + 1);
        }

        for (int e: rem.keySet()) {
            int dif = k - e;
            
            if (e == 0) {
                if (rem.get(e) % 2 != 0)
                    return false;
            }
            else if (rem.containsKey(dif)) {
                if (rem.get(e) != rem.get(dif))
                    return false;
            }
            else {
                return false;
            }
        }
        return true;
    }
}
 
 
0
patilamrut1793 weeks ago
javascriptclass Solution {   canPair(nums,k){       //code here       const len = nums.length;       if(len%2!==0) return false;       let pairs = 0;       const hash = {};       for(let i=0; i<len; i++) {           const mod = nums[i]%k;           if(mod === 0 && hash[mod]) {               pairs++;               hash[mod]--;               if(hash[mod]===0) delete hash[mod];           } else if(hash[k-mod]) {               pairs++;               hash[k-mod]--;               if(hash[k-mod]===0) delete hash[k-mod];           } else {               hash[mod] = (hash[mod] || 0)+1;           }       }       return (len/2)===pairs;   }}
0
lindan1233 weeks ago
bool canPair(vector<int> nums, int k) {
        
        unordered_map<int,int> mp;
        if(nums.size()%2!=0)
        {
            return false;
        }
        
        for(int i=0;i<nums.size();i++)
        {
            mp[nums[i]%k]++;
        }
        
        for(auto x : mp)
        {
            int rem = k-x.first;
            
            if(x.first==0)
            {
                if(mp[0]%2!=0)return false;
            }
            
            else if(mp[x.first]!=mp[rem]) return false;
        }
        return true;
    }
Time Taken : 0.22sec
Cpp
0
imranwahid3 weeks ago
Easy C++ solution
0
amrit_kumar3 weeks ago
c++ simple soln
 
bool canPair(vector<int> nums, int k) 
    {
      int ans=0;
      
      if(nums.size()%2!=0) return false;
      unordered_map<int,int> mp;
      
      for(int i=0;i<nums.size();i++)
      mp[nums[i]%k]++;
      
      for(auto i = mp.begin();i!=mp.end();i++)
      {
          int temp = k-i->first;
          if(i->first==0)
          {
              if(mp[0]%2!=0) return false;
          }
          
          else if(mp[i->first]!=mp[temp]) return false;
          
         
        }
      
      return true;
      
    }
0
himanshu201911 month ago
int n=nums.size();       if(n%2!=0) return false;       vector<int> vec;       for(int i=0;i<n;i++){           vec.push_back(nums[i]%k);       }       unordered_map<int,int> m;       for(int i=0;i<n;i++){           m[vec[i]]++;       }       for(int i=0;i<n;i++){           if(vec[i]==0){               if(m[vec[i]]%2!=0) return false;           }           else if(2*vec[i]==k){               if(m[vec[i]]%2!=0) return false;           }           else{               if(m[vec[i]]!=m[k-vec[i]]) return false;           }       }       return true;
+3
mayank_manas1 month ago
this shouldn't be in the easy section.
-1
abhishektiwari162 months ago
class Solution {
    boolean flag = false;
   
    public boolean canPair(int[] nums, int k) {
        // Code here
        HashSet<Integer> set = new HashSet<>();
        
        HashMap<Integer,Integer> map = new HashMap<>();
        
        
      
        int key;
        for(int i: nums){
         
            key = (k-(i%k))%k;
            if(map.containsKey(key)){
                map.put(key, map.get(key) - 1);
                if(map.get(key) == 0)
                    map.remove(key);
            }
            else{
                key = i%k;
                if(map.containsKey(key)) map.put(key, map.get(key) +1);
                else
                map.put(key, 1);
            }
        }
       
        if(map.size() == 0)
        return true;
        
        return false;
    }
}
0
ayushsunariya4582 months ago
 bool canPair(vector<int> nums, int k) {
        // Code here.
        vector<int>res(k,0);
        for(auto &x:nums){
            x%=k;
            res[x]++;
        }

        for(auto x:nums){
            if(x==0){
                if(res[0]%2==0){
                    continue;
                }
                else return false;
            }
            if(res[k-x]>0){
                res[k-x]--;
            }
            else return false;
        }
        return true;
    }
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.