Make all array elements equal by reducing array elements to half minimum number of times | Function to find minimum number of operations ; Initialize map ; Traverse the array ; Divide current array element until it reduces to 1 ; Traverse the map ; Find the maximum element having frequency equal to N ; Stores the minimum number of operations required ; Count operations required to convert current element to mx ; Print the answer ; Given array ; Size of the array
def minOperations ( arr , N ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT res = arr [ i ] NEW_LINE while ( res ) : NEW_LINE if res in mp : NEW_LINE INDENT mp [ res ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ res ] = 1 NEW_LINE DEDENT res //= 2 NEW_LINE DEDENT mx = 1 NEW_LINE for it in mp : NEW_LINE INDENT if ( mp [ it ] == N ) : NEW_LINE mx = it NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT res = arr [ i ] NEW_LINE while ( res != mx ) : NEW_LINE ans += 1 NEW_LINE res //= 2 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 3 , 1 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE minOperations ( arr , N ) NEW_LINE