Given an array of integer numbers, we need to find maximum size of a subset such that sum of each pair of this subset is not divisible by K.

**Examples :**

Input : arr[] = [3, 7, 2, 9, 1] K = 3 Output : 3 Maximum size subset whose each pair sum is not divisible by K is [3, 7, 1] because, 3+7 = 10, 3+1 = 4, 7+1 = 8 all are not divisible by 3. It is not possible to get a subset of size bigger than 3 with the above-mentioned property. Input : arr[] = [3, 17, 12, 9, 11, 15] K = 5 Output : 4

We can solve this problem by computing modulo of array numbers with K. if sum of two numbers is divisible by K, then if one of them gives remainder i, other will give remainder (K – i). First we store frequencies of numbers giving specific remainder in a frequency array of size K. Then we loop for all remainders i and include max(f[i], f[K – i]). Why? a subset with no pair sum divisible by K must include either elements with remainder f[i] or with remainder f[K – i]. Since we want to maximize the size of subset, we pick maximum of two sizes.

In below code array numbers with remainder 0 and remainder K/2 are handled separately. If we include more than 2 numbers with remainder 0 then their sum will be divisible by K, so we have taken at max 1 number in our consideration, same is the case with array numbers giving remainder K/2.

## C++

`// C++ program to get size of subset whose ` `// each pair sum is not divisible by K ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns maximum size of subset with no pair ` `// sum divisible by K ` `int` `subsetPairNotDivisibleByK(` `int` `arr[], ` `int` `N, ` ` ` `int` `K) ` `{ ` ` ` `// Array for storing frequency of modulo ` ` ` `// values ` ` ` `int` `f[K]; ` ` ` `memset` `(f, 0, ` `sizeof` `(f)); ` ` ` ` ` `// Fill frequency array with values modulo K ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `f[arr[i] % K]++; ` ` ` ` ` `// if K is even, then update f[K/2] ` ` ` `if` `(K % 2 == 0) ` ` ` `f[K/2] = min(f[K/2], 1); ` ` ` ` ` `// Initialize result by minimum of 1 or ` ` ` `// count of numbers giving remainder 0 ` ` ` `int` `res = min(f[0], 1); ` ` ` ` ` `// Choose maximum of count of numbers ` ` ` `// giving remainder i or K-i ` ` ` `for` `(` `int` `i = 1; i <= K/2; i++) ` ` ` `res += max(f[i], f[K-i]); ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `arr[] = {3, 7, 2, 9, 1}; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `K = 3; ` ` ` `cout << subsetPairNotDivisibleByK(arr, N, K); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to get size of subset whose ` `// each pair sum is not divisible by K ` `import` `java.util.Arrays; ` ` ` `class` `Test { ` ` ` ` ` `// Returns maximum size of subset with no pair ` ` ` `// sum divisible by K ` ` ` `static` `int` `subsetPairNotDivisibleByK(` `int` `arr[], ` ` ` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` ` ` `// Array for storing frequency of modulo ` ` ` `// values ` ` ` `int` `f[] = ` `new` `int` `[K]; ` ` ` `Arrays.fill(f, ` `0` `); ` ` ` ` ` `// Fill frequency array with values modulo K ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `f[arr[i] % K]++; ` ` ` ` ` `// if K is even, then update f[K/2] ` ` ` `if` `(K % ` `2` `== ` `0` `) ` ` ` `f[K/` `2` `] = Math.min(f[K/` `2` `], ` `1` `); ` ` ` ` ` `// Initialize result by minimum of 1 or ` ` ` `// count of numbers giving remainder 0 ` ` ` `int` `res = Math.min(f[` `0` `], ` `1` `); ` ` ` ` ` `// Choose maximum of count of numbers ` ` ` `// giving remainder i or K-i ` ` ` `for` `(` `int` `i = ` `1` `; i <= K/` `2` `; i++) ` ` ` `res += Math.max(f[i], f[K-i]); ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `arr[] = {` `3` `, ` `7` `, ` `2` `, ` `9` `, ` `1` `}; ` ` ` `int` `N = arr.length; ` ` ` `int` `K = ` `3` `; ` ` ` ` ` `System.out.print(subsetPairNotDivisibleByK( ` ` ` `arr, N, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to get size of ` `# subset whose each pair sum is ` `# not divisible by K ` ` ` `# Returns maximum size of subset ` `# with no pair sum divisible by K ` `def` `subsetPairNotDivisibleByK(arr, N, K): ` ` ` ` ` `# Array for storing frequency ` ` ` `# of modulo values ` ` ` `f ` `=` `[` `0` `for` `i ` `in` `range` `(K)] ` ` ` ` ` `# Fill frequency array with ` ` ` `# values modulo K ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `f[arr[i] ` `%` `K] ` `+` `=` `1` ` ` ` ` `# if K is even, then update f[K/2] ` ` ` `if` `(K ` `%` `2` `=` `=` `0` `): ` ` ` `f[K` `/` `/` `2` `] ` `=` `min` `(f[K` `/` `/` `2` `], ` `1` `) ` ` ` ` ` `# Initialize result by minimum of 1 or ` ` ` `# count of numbers giving remainder 0 ` ` ` `res ` `=` `min` `(f[` `0` `], ` `1` `) ` ` ` ` ` `# Choose maximum of count of numbers ` ` ` `# giving remainder i or K-i ` ` ` `for` `i ` `in` `range` `(` `1` `,(K ` `/` `/` `2` `) ` `+` `1` `): ` ` ` `res ` `+` `=` `max` `(f[i], f[K ` `-` `i]) ` ` ` ` ` `return` `res ` ` ` `# Driver Code ` `arr ` `=` `[` `3` `, ` `7` `, ` `2` `, ` `9` `, ` `1` `] ` `N ` `=` `len` `(arr) ` `K ` `=` `3` `print` `(subsetPairNotDivisibleByK(arr, N, K)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to get size of subset whose ` `// each pair sum is not divisible by K ` `using` `System; ` ` ` `class` `Test { ` ` ` ` ` `// Returns maximum size of subset ` ` ` `// with no pair sum divisible by K ` ` ` `static` `int` `subsetPairNotDivisibleByK(` `int` `[]arr, ` ` ` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `// Array for storing ` ` ` `// frequency of modulo values ` ` ` `int` `[]f = ` `new` `int` `[K]; ` ` ` `for` `(` `int` `i = 0; i < K; i++) ` ` ` `f[i] = 0; ` ` ` ` ` ` ` `// Fill frequency array with values modulo K ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `f[arr[i] % K]++; ` ` ` ` ` `// if K is even, then update f[K/2] ` ` ` `if` `(K % 2 == 0) ` ` ` `f[K/2] = Math.Min(f[K/2], 1); ` ` ` ` ` `// Initialize result by minimum of 1 or ` ` ` `// count of numbers giving remainder 0 ` ` ` `int` `res = Math.Min(f[0], 1); ` ` ` ` ` `// Choose maximum of count of numbers ` ` ` `// giving remainder i or K-i ` ` ` `for` `(` `int` `i = 1; i <= K/2; i++) ` ` ` `res += Math.Max(f[i], f[K-i]); ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = {3, 7, 2, 9, 1}; ` ` ` `int` `N = arr.Length; ` ` ` `int` `K = 3; ` ` ` ` ` `// Function calling ` ` ` `Console.Write(subsetPairNotDivisibleByK(arr, N, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to get size of subset whose ` `// each pair sum is not divisible by K ` ` ` `// Returns maximum size of subset with ` `// no pair sum divisible by K ` `function` `subsetPairNotDivisibleByK(&` `$arr` `, ` `$N` `, ` `$K` `) ` `{ ` ` ` `// Array for storing frequency of ` ` ` `// modulo values ` ` ` `$f` `= ` `array_fill` `(0, ` `$K` `, NULL); ` ` ` ` ` `// Fill frequency array with ` ` ` `// values modulo K ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$N` `; ` `$i` `++) ` ` ` `$f` `[` `$arr` `[` `$i` `] % ` `$K` `]++; ` ` ` ` ` `// if K is even, then update f[K/2] ` ` ` `if` `(` `$K` `% 2 == 0) ` ` ` `$f` `[` `$K` `/ 2] = min(` `$f` `[` `$K` `/ 2], 1); ` ` ` ` ` `// Initialize result by minimum of 1 or ` ` ` `// count of numbers giving remainder 0 ` ` ` `$res` `= min(` `$f` `[0], 1); ` ` ` ` ` `// Choose maximum of count of numbers ` ` ` `// giving remainder i or K-i ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$K` `/ 2; ` `$i` `++) ` ` ` `$res` `+= max(` `$f` `[` `$i` `], ` `$f` `[` `$K` `- ` `$i` `]); ` ` ` ` ` `return` `$res` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(3, 7, 2, 9, 1); ` `$N` `= sizeof(` `$arr` `); ` `$K` `= 3; ` `echo` `subsetPairNotDivisibleByK(` `$arr` `, ` `$N` `, ` `$K` `); ` ` ` `// This code is contributed by ita_c ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N + K)

**Auxiliary Space:** O(K)

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Largest subset with sum of every pair as prime
- Subset with sum divisible by m
- Pair formation such that maximum pair sum is minimized
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Largest divisible subset in array
- Size of the largest divisible subset in an Array
- Largest divisible pairs subset
- Minimum pair sum operations to make array each element divisible by 4
- Subarray with no pair sum divisible by K
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Largest subset having with sum less than equal to sum of respective indices
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
- Pair with given product | Set 1 (Find if any pair exists)
- Probability of a random pair being the maximum weighted pair
- Store duplicate keys-values pair and sort the key-value pair by key
- Triplet pair (a, b, c) such that a+b, b+c and a+c are all divisible by K
- Check if there is any pair in a given range with GCD is divisible by k
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1