- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

Suppose we have an array of numbers and have another number k, we have to check whether given array can be divided into pairs such that the sum of every pair is divisible by k or not.

So, if the input is like arr = [5, 15, 6, 9] k = 7, then the output will be True as we can take pairs like (5, 9) and (15, 6).

To solve this, we will follow these steps −

- n := size of array
- if n is odd, then
- return False

- occurrences := an empty dictionary, if some key is not there return 0 as the value of that missing key
- for i in range 0 to n, do
- increase occurrences[((array[i] mod k) + k) mod k] by 1

- for i in range 0 to n, do
- remainder := ((array[i] mod k) + k) mod k
- if 2 * remainder is same as k, then
- if occurrences[remainder] is odd, then
- return False

- if occurrences[remainder] is odd, then
- otherwise when remainder is same as 0, then
- if occurrences[remainder] AND 1 is non-zero, then
- return False

- if occurrences[remainder] AND 1 is non-zero, then
- otherwise when occurrences[remainder] is not same as occurrences[k - remainder], then
- return False

- return True

Let us see the following implementation to get better understanding −

## Example

from collections import defaultdict def solve(array, k): n = len(array) if n % 2 != 0: return False occurrences = defaultdict(lambda : 0) for i in range(0, n): occurrences[((array[i] % k) + k) % k] += 1 for i in range(0, n): remainder = ((array[i] % k) + k) % k if (2 * remainder == k): if (occurrences[remainder] % 2 != 0): return False elif (remainder == 0): if (occurrences[remainder] & 1): return False elif (occurrences[remainder] != occurrences[k - remainder]): return False return True arr = [5, 15, 6, 9] k = 7 print(solve(arr, k))

## Input

[5, 15, 6, 9], 7

## Output

True

- Related Articles
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Count pairs in array whose sum is divisible by K in C++
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python
- Program to check if array pairs are divisible by k or not using Python
- Count pairs in array whose sum is divisible by 4 in C++
- Check if array sum can be made K by three operations on it in Python
- Program to check whether list can be partitioned into pairs where sum is multiple of k in python
- Find if array can be divided into two subarrays of equal sum in C++
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python
- Find the sums for which an array can be divided into subarrays of equal sum in Python
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Check if a large number can be divided into two or more segments of equal sum in C++
- Check if any square (with one colored cell) can be divided into two equal parts in Python

Advertisements