Minimum operations to make counts of remainders same in an array | C # implementation of the above approach ; Function to return the minimum number of operations required ; To store modulos values ; If it 's size greater than k  it needed to be decreased ; If it 's size is less than k  it needed to be increased ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int minOperations ( int n , int [ ] a , int m ) { int k = n / m ; List < int > [ ] val = new List < int > [ m ] ; for ( int i = 0 ; i < val . Length ; i ++ ) val [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { val [ a [ i ] % m ] . Add ( i ) ; } long ans = 0 ; List < pair > extra = new List < pair > ( ) ; for ( int i = 0 ; i < 2 * m ; ++ i ) { int cur = i % m ; while ( ( val [ cur ] . Count ) > k ) { int elem = val [ cur ] [ val [ cur ] . Count - 1 ] ; val [ cur ] . RemoveAt ( val [ cur ] . Count - 1 ) ; extra . Add ( new pair ( elem , i ) ) ; } while ( val [ cur ] . Count < k && extra . Count != 0 ) { int elem = extra [ extra . Count - 1 ] . first ; int mmod = extra [ extra . Count - 1 ] . second ; extra . RemoveAt ( extra . Count - 1 ) ; val [ cur ] . Add ( elem ) ; ans += i - mmod ; } } return ( int ) ans ; } public static void Main ( String [ ] args ) { int m = 3 ; int [ ] a = { 3 , 2 , 0 , 6 , 10 , 12 } ; int n = a . Length ; Console . Write ( minOperations ( n , a , m ) ) ; } }