Minimum operations required to make all the array elements equal | Function to return the minimum number of given operation required to make all the array elements equal ; Check if all the elements from kth index to last are equal ; Finding the 1 st element which is not equal to the kth element ; Driver code
def minOperation ( n , k , a ) : NEW_LINE INDENT for i in range ( k , n ) : NEW_LINE INDENT if ( a [ i ] != a [ k - 1 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT for i in range ( k - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] != a [ k - 1 ] ) : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT DEDENT n = 5 NEW_LINE k = 3 NEW_LINE a = [ 2 , 1 , 1 , 1 , 1 ] NEW_LINE print ( minOperation ( n , k , a ) ) NEW_LINE