Minimum operations to make counts of remainders same in an array | 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
def minOperations ( n , a , m ) : NEW_LINE INDENT k = n // m NEW_LINE val = [ [ ] for i in range ( m ) ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT val [ a [ i ] % m ] . append ( i ) NEW_LINE DEDENT ans = 0 NEW_LINE extra = [ ] NEW_LINE for i in range ( 0 , 2 * m ) : NEW_LINE INDENT cur = i % m NEW_LINE while len ( val [ cur ] ) > k : NEW_LINE INDENT elem = val [ cur ] . pop ( ) NEW_LINE extra . append ( ( elem , i ) ) NEW_LINE DEDENT while ( len ( val [ cur ] ) < k and len ( extra ) > 0 ) : NEW_LINE INDENT elem = extra [ - 1 ] [ 0 ] NEW_LINE mmod = extra [ - 1 ] [ 1 ] NEW_LINE extra . pop ( ) NEW_LINE val [ cur ] . append ( elem ) NEW_LINE ans += i - mmod NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = 3 NEW_LINE a = [ 3 , 2 , 0 , 6 , 10 , 12 ] NEW_LINE n = len ( a ) NEW_LINE print ( minOperations ( n , a , m ) ) NEW_LINE DEDENT