Minimum operations required to make all elements in an array of first N odd numbers equal | Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment the sum by the value arr [ i ] ; Stores the middle element ; If N is even ; Otherwise ; Stores the result ; Traverse the range [ 0 , N / 2 ] ; Update the value of ans ; Return the result ; Driver Code
def minOperations ( N ) : NEW_LINE INDENT arr = [ 0 ] * N NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT arr [ i ] = ( 2 * i ) + 1 NEW_LINE sum = sum + arr [ i ] NEW_LINE DEDENT mid = 0 NEW_LINE if N % 2 == 0 : NEW_LINE INDENT mid = sum / N NEW_LINE DEDENT else : NEW_LINE INDENT mid = arr [ int ( N / 2 ) ] NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( int ( N / 2 ) ) : NEW_LINE INDENT ans += mid - arr [ i ] NEW_LINE DEDENT return int ( ans ) NEW_LINE DEDENT N = 6 NEW_LINE print ( minOperations ( N ) ) NEW_LINE