# Check if an array can be divided into pairs whose sum is divisible by k

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.

**Examples:**

Input: arr[] = {9, 7, 5, 3},

k = 6

Output: True

We can divide array into (9, 3) and

(7, 5). Sum of both of these pairs

is a multiple of 6.

Input: arr[] = {92, 75, 65, 48, 45, 35},

k = 10

Output: True

We can divide array into (92, 48), (75, 65)

and (45, 35). Sum of all these pairs is a

multiple of 10.

Input: arr[] = {91, 74, 66, 48}, k = 10

Output: False

A **Simple Solution** is to iterate through every element arr[i]. Find if there is another not yet visited element that has remainder as **(k – arr[i]%k)**. If there is no such element, return false. If a pair is found, then mark both elements as visited.

Time complexity of this solution is O(n^{2} and it requires O(n) extra space.

An **Efficient Solution** is to use Hashing.

1) If length of given array is odd, return false. An odd length array cannot be divided into pairs. 2) Traverse input array and count occurrences of all reminders. freq[arr[i] % k]++ 3) Traverse input array again. a) Find the remainder of the current element. b) If remainder divides k into two halves, then there must be even occurrences of it as it forms pair with itself only. c) If the remainder is 0, then there must be even occurrences. c) Else, number of occurrences of current the remainder must be equal to a number of occurrences of "k - current remainder".

An efficient approach is to use a map in C++ STL. The map is typically implemented using Red-Black Tree and takes O(Log n) time for access. Therefore time complexity of below implementation is O(n Log n), but the algorithm can be easily implemented in O(n) time using a hash table.

Below image is a dry run of the above aprroach:

Below is th eimplemntation of the above approach:

## C++

`// A C++ program to check if arr[0..n-1] can be divided ` `// in pairs such that every pair is divisible by k. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns true if arr[0..n-1] can be divided into pairs ` `// with sum divisible by k. ` `bool` `canPairs(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// An odd length array cannot be divided into pairs ` ` ` `if` `(n & 1) ` ` ` `return` `false` `; ` ` ` ` ` `// Create a frequency array to count occurrences ` ` ` `// of all remainders when divided by k. ` ` ` `map<` `int` `, ` `int` `> freq; ` ` ` ` ` `// Count occurrences of all remainders ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `freq[arr[i] % k]++; ` ` ` ` ` `// Traverse input array and use freq[] to decide ` ` ` `// if given array can be divided in pairs ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// Remainder of current element ` ` ` `int` `rem = arr[i] % k; ` ` ` ` ` `// If remainder with current element divides ` ` ` `// k into two halves. ` ` ` `if` `(2*rem == k) ` ` ` `{ ` ` ` `// Then there must be even occurrences of ` ` ` `// such remainder ` ` ` `if` `(freq[rem] % 2 != 0) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// If remainder is 0, then there must be two ` ` ` `// elements with 0 remainder ` ` ` `else` `if` `(rem == 0) ` ` ` `{ ` ` ` `if` `(freq[rem] & 1) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Else number of occurrences of remainder ` ` ` `// must be equal to number of occurrences of ` ` ` `// k - remainder ` ` ` `else` `if` `(freq[rem] != freq[k - rem]) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `/* Driver program to test above function */` `int` `main() ` `{ ` ` ` `int` `arr[] = {92, 75, 65, 48, 45, 35}; ` ` ` `int` `k = 10; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `canPairs(arr, n, k)? cout << ` `"True"` `: cout << ` `"False"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

` ` `import` `java.util.HashMap; ` ` ` `public` `class` `Divisiblepair ` `{ ` ` ` `// Returns true if arr[0..n-1] can be divided into pairs ` ` ` `// with sum divisible by k. ` ` ` `static` `boolean` `canPairs(` `int` `ar[], ` `int` `k) ` ` ` `{ ` ` ` `// An odd length array cannot be divided into pairs ` ` ` `if` `(ar.length % ` `2` `== ` `1` `) ` ` ` `return` `false` `; ` ` ` ` ` `// Create a frequency array to count occurrences ` ` ` `// of all remainders when divided by k. ` ` ` `HashMap<Integer, Integer> hm = ` `new` `HashMap<>(); ` ` ` ` ` `// Count occurrences of all remainders ` ` ` `for` `(` `int` `i = ` `0` `; i < ar.length; i++) ` ` ` `{ ` ` ` `int` `rem = ar[i] % k; ` ` ` `if` `(!hm.containsKey(rem)) ` ` ` `{ ` ` ` `hm.put(rem, ` `0` `); ` ` ` `} ` ` ` `hm.put(rem, hm.get(rem) + ` `1` `); ` ` ` `} ` ` ` ` ` `// Traverse input array and use freq[] to decide ` ` ` `// if given array can be divided in pairs ` ` ` `for` `(` `int` `i = ` `0` `; i < ar.length; i++) ` ` ` `{ ` ` ` `// Remainder of current element ` ` ` `int` `rem = ar[i] % k; ` ` ` ` ` `// If remainder with current element divides ` ` ` `// k into two halves. ` ` ` `if` `(` `2` `* rem == k) ` ` ` `{ ` ` ` `// Then there must be even occurrences of ` ` ` `// such remainder ` ` ` `if` `(hm.get(rem) % ` `2` `== ` `1` `) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// If remainder is 0, then there must be two ` ` ` `// elements with 0 remainder ` ` ` `else` `if` `(rem == ` `0` `) ` ` ` `{ ` ` ` `// Then there must be even occurrences of ` ` ` `// such remainder ` ` ` `if` `(hm.get(rem) % ` `2` `== ` `1` `) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Else number of occurrences of remainder ` ` ` `// must be equal to number of occurrences of ` ` ` `// k - remainder ` ` ` `else` ` ` `{ ` ` ` `if` `(hm.get(k - rem) != hm.get(rem)) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver program to test above functions ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `92` `, ` `75` `, ` `65` `, ` `48` `, ` `45` `, ` `35` `}; ` ` ` `int` `k = ` `10` `; ` ` ` `boolean` `ans = canPairs(arr, k); ` ` ` `if` `(ans) ` ` ` `System.out.println(` `"True"` `); ` ` ` `else` ` ` `System.out.println(` `"False"` `); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Rishabh Mahrsee ` |

*chevron_right*

*filter_none*

Output:

True

**Time complexity :** O(n).

This article is contributed by **Priyanka**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Check if a sorted array can be divided in pairs whose sum is k
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Count divisible pairs in an array
- Count pairs in array whose sum is divisible by K
- Count pairs in array whose sum is divisible by 4
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Check if the sum of primes is divisible by any prime from the array
- Check if the sum of perfect squares in an array is divisible by x
- Find if array can be divided into two subarrays of equal sum
- Find reminder of array multiplication divided by n
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Find the sums for which an array can be divided into sub-arrays of equal sum
- No of pairs (a[j] >= a[i]) with k numbers in range (a[i], a[j]) that are divisible by x
- Largest divisible pairs subset