Check if an array can be divided into pairs whose sum is divisible by k | C # program to check if arr [ 0. . n - 1 ] can be divided in pairs such that every pair is divisible by k . ; Returns true if arr [ 0. . n - 1 ] can be divided into pairs with sum divisible by k . ; An odd length array cannot be divided into pairs ; Create a frequency array to count occurrences of all remainders when divided by k . ; Count occurrences of all remainders ; Traverse input array and use freq [ ] to decide if given array can be divided in pairs ; Remainder of current element ; If remainder with current element divides k into two halves . ; Then there must be even occurrences of such remainder ; If remainder is 0 , then there must be two elements with 0 remainder ; Then there must be even occurrences of such remainder ; Else number of occurrences of remainder must be equal to number of occurrences of k - remainder ; Driver code ; Function call
using System . Collections . Generic ; using System ; class GFG { static bool canPairs ( int [ ] ar , int k ) { if ( ar . Length % 2 == 1 ) return false ; Dictionary < Double , int > hm = new Dictionary < Double , int > ( ) ; for ( int i = 0 ; i < ar . Length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( ! hm . ContainsKey ( rem ) ) { hm [ rem ] = 0 ; } hm [ rem ] ++ ; } for ( int i = 0 ; i < ar . Length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( 2 * rem == k ) { if ( hm [ rem ] % 2 == 1 ) return false ; } else if ( rem == 0 ) { if ( hm [ rem ] % 2 == 1 ) return false ; } else { if ( hm [ k - rem ] != hm [ rem ] ) return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 92 , 75 , 65 , 48 , 45 , 35 } ; int k = 10 ; bool ans = canPairs ( arr , k ) ; if ( ans ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }