Minimum operations required to make all Array elements divisible by K | C # implementation to find the minimum number of moves required to update the array such that each of its element is divisible by K ; Function to find the minimum number of moves required to update the array such that each of its element is divisible by K ; Initialize Map data structure ; Iterate for all the elements of the array ; Calculate the value to be added ; Check if the value equals to 0 then simply continue ; Check if the value to be added is present in the map ; Update the answer ; Print the required result We need to add 1 to maxX because we cant ignore the first move where initially X = 0 and we need to increase it by 1 to make some changes in array ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void compute ( int [ ] a , int N , int K ) { Dictionary < long , long > eqVal = new Dictionary < long , long > ( ) ; long maxX = 0 ; for ( int i = 0 ; i < N ; i ++ ) { long val = a [ i ] % K ; val = ( val == 0 ? 0 : K - val ) ; if ( val == 0 ) continue ; if ( eqVal . ContainsKey ( val ) ) { long numVal = eqVal [ val ] ; maxX = Math . Max ( maxX , val + ( K * numVal ) ) ; eqVal [ val ] = 1 + eqVal . GetValueOrDefault ( val , 0 ) ; } else { eqVal . Add ( val , 1 ) ; maxX = Math . Max ( maxX , val ) ; } } Console . Write ( maxX == 0 ? 0 : maxX + 1 ) ; } public static void Main ( string [ ] args ) { int K = 3 ; int [ ] a = { 1 , 2 , 2 , 18 } ; int N = a . Length ; compute ( a , N , K ) ; } }