Given an integer array and two numbers k and m. Print k numbers from the array, such that difference between any two pairs is divisible by m. If there are no k numbers, then print -1.

**Examples:**

Input: arr[] = {1, 8, 4} k = 2 m = 3 Output: 1 4 Explanation: Difference between 1 and 4 is divisible by 3. Input: arr[] = {1, 8, 4} k = 3 m = 3 Output: -1 Explanation: there are only two numbers whose difference is divisible by m, but k is three here which is not possible, hence we print -1.

A **naive approach ** is be to iterate for every element and check with all other elements, and if the count of numbers whose difference is divisible by m is greater then equal to k, then we print those k numbers by again iterating. But this won’t be efficient enough as it runs two nested loops.

Time complexity: O(n * n)

Auxiliary space: O(1)

An **efficient approach** is apply a mathematical approach, where we know if (x-y) % m is equal to x%m – y%m. So if we can store all the numbers who leave the same remainder when divided by m,

And if the count of numbers which leaves the same remainder is more than k or equal to k, then we have our answer as all those numbers who leave the same remainder.

Below is the implementation of the above approach

## C++

`// CPP program to find a list of k elements from ` `// an array such that difference between all of ` `// them is divisible by m. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to generate k numbers whose difference ` `// is divisible by m ` `void` `print_result(` `int` `a[], ` `int` `n, ` `int` `k, ` `int` `m) ` `{ ` ` ` `// Using an adjacency list like representation ` ` ` `// to store numbers that lead to same ` ` ` `// remainder. ` ` ` `vector<` `int` `> v[m]; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// stores the modulus when divided ` ` ` `// by m ` ` ` `int` `rem = a[i] % m; ` ` ` ` ` `v[rem].push_back(a[i]); ` ` ` ` ` `// If we found k elements which ` ` ` `// have same remainder. ` ` ` `if` `(v[rem].size() == k) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < k; j++) ` ` ` `cout << v[rem][j] << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we could not find k elements ` ` ` `cout << ` `"-1"` `; ` `} ` ` ` `// driver program to test the above function ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 8, 4 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `print_result(a, n, 2, 3); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find a list of k elements from ` `// an array such that difference between all of ` `// them is divisible by m. ` `import` `java.util.*; ` `class` `GFG ` `{ ` ` ` `// function to generate k numbers whose difference ` `// is divisible by m ` `static` `void` `print_result(` `int` `a[], ` `int` `n, ` ` ` `int` `k, ` `int` `m) ` `{ ` ` ` `// Using an adjacency list like representation ` ` ` `// to store numbers that lead to same ` ` ` `// remainder. ` ` ` `Vector<Vector<Integer>> v = ` `new` `Vector<Vector<Integer>>(m); ` ` ` `for` `(` `int` `i = ` `0` `; i < m; i++) ` ` ` `v.add(` `new` `Vector<Integer>()); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// stores the modulus when divided ` ` ` `// by m ` ` ` `int` `rem = a[i] % m; ` ` ` ` ` `v.get(rem).add(a[i]); ` ` ` ` ` `// If we found k elements which ` ` ` `// have same remainder. ` ` ` `if` `(v.get(rem).size() == k) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < k; j++) ` ` ` `System.out.print(v.get(rem).get(j) + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we could not find k elements ` ` ` `System.out.print(` `"-1"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `8` `, ` `4` `}; ` ` ` `int` `n = a.length; ` ` ` `print_result(a, n, ` `2` `, ` `3` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find a list of k elements from ` `# an array such that difference between all of ` `# them is divisible by m. ` ` ` `# function to generate k numbers whose difference ` `# is divisible by m ` `def` `print_result(a, n, k, m): ` ` ` ` ` `# Using an adjacency list like representation ` ` ` `# to store numbers that lead to same ` ` ` `# remainder. ` ` ` `v ` `=` `[[] ` `for` `i ` `in` `range` `(m)] ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` ` ` `# stores the modulus when divided ` ` ` `# by m ` ` ` `rem ` `=` `a[i] ` `%` `m ` ` ` ` ` `v[rem].append(a[i]) ` ` ` ` ` `# If we found k elements which ` ` ` `# have same remainder. ` ` ` `if` `(` `len` `(v[rem]) ` `=` `=` `k): ` ` ` ` ` `for` `j ` `in` `range` `(` `0` `, k): ` ` ` `print` `(v[rem][j], end` `=` `" "` `) ` ` ` `return` ` ` ` ` `# If we could not find k elements ` ` ` `print` `(` `-` `1` `) ` ` ` `# driver program to test the above function ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `[` `1` `, ` `8` `, ` `4` `] ` ` ` `n ` `=` `len` `(a) ` ` ` `print_result(a, n, ` `2` `, ` `3` `) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find a list of k elements from ` `// an array such that difference between all of ` `// them is divisible by m. ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// function to generate k numbers whose difference ` `// is divisible by m ` `static` `void` `print_result(` `int` `[]a, ` `int` `n, ` ` ` `int` `k, ` `int` `m) ` `{ ` ` ` `// Using an adjacency list like representation ` ` ` `// to store numbers that lead to same ` ` ` `// remainder. ` ` ` `List<List<` `int` `>> v = ` `new` `List<List<` `int` `>>(m); ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `v.Add(` `new` `List<` `int` `>()); ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// stores the modulus when divided ` ` ` `// by m ` ` ` `int` `rem = a[i] % m; ` ` ` ` ` `v[rem].Add(a[i]); ` ` ` ` ` `// If we found k elements which ` ` ` `// have same remainder. ` ` ` `if` `(v[rem].Count == k) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < k; j++) ` ` ` `Console.Write(v[rem][j] + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we could not find k elements ` ` ` `Console.Write(` `"-1"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]a = { 1, 8, 4 }; ` ` ` `int` `n = a.Length; ` ` ` `print_result(a, n, 2, 3); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find a list of k elements ` `// from an array such that difference between ` `// all of them is divisible by m. ` ` ` `// function to generate k numbers whose ` `// difference is divisible by m ` `function` `print_result(` `$a` `, ` `$n` `, ` `$k` `, ` `$m` `) ` `{ ` ` ` `// Using an adjacency list like representation ` ` ` `// to store numbers that lead to same ` ` ` `// remainder. ` ` ` `$v` `= ` `array_fill` `(0, ` `$m` `+ 1, ` `array` `()); ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// stores the modulus when divided ` ` ` `// by m ` ` ` `$rem` `= ` `$a` `[` `$i` `] % ` `$m` `; ` ` ` ` ` `array_push` `(` `$v` `[` `$rem` `], ` `$a` `[` `$i` `]); ` ` ` ` ` `// If we found k elements which ` ` ` `// have same remainder. ` ` ` `if` `(` `count` `(` `$v` `[` `$rem` `]) == ` `$k` `) ` ` ` `{ ` ` ` `for` `(` `$j` `= 0; ` `$j` `< ` `$k` `; ` `$j` `++) ` ` ` `echo` `$v` `[` `$rem` `][` `$j` `] . ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we could not find k elements ` ` ` `echo` `"-1"` `; ` `} ` ` ` `// Driver Code ` `$a` `= ` `array` `( 1, 8, 4 ); ` `$n` `= ` `count` `(` `$a` `); ` `print_result(` `$a` `, ` `$n` `, 2, 3); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1 4

**Time complexity:** O(n)

**Auxiliary Space:** O(m)

This article is contributed by **Raja Vikramaditya**. 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:

- 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]
- Program to print all the numbers divisible by 3 and 5 for a given number
- Count numbers which are divisible by all the numbers from 2 to 10
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Length of longest subsequence having absolute difference of all pairs divisible by K
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find permutation of n which is divisible by 3 but not divisible by 6
- Count of numbers upto M divisible by given Prime Numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print all pairs in an unsorted array with equal sum
- Sum of all numbers divisible by 6 in a given range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Sum and Product of all Composite numbers which are divisible by k in an array
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Sum of all numbers in the given range which are divisible by M
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Largest divisible pairs subset