Print k numbers where all pairs are divisible by m | C # 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 ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void print_result ( int [ ] a , int n , int k , int m ) { 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 ++ ) { int rem = a [ i ] % m ; v [ rem ] . Add ( a [ i ] ) ; if ( v [ rem ] . Count == k ) { for ( int j = 0 ; j < k ; j ++ ) Console . Write ( v [ rem ] [ j ] + " ▁ " ) ; return ; } } Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 8 , 4 } ; int n = a . Length ; print_result ( a , n , 2 , 3 ) ; } }