Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | Function to prthe index from where the operation can be started ; Initialize B ; Initialize save ; Make B equals to arr ; Sort the array B ; Traverse from N - 1 to 1 ; If B [ i ] & B [ i - 1 ] are unequal ; If all elements are same ; If arr [ 1 ] is maximum element ; If arr [ N - 1 ] is maximum element ; Find the maximum element ; Driver Code ; Given array arr ; Length of array ; Function Call
def printIndex ( arr , N ) : NEW_LINE INDENT B = [ 0 ] * ( N ) NEW_LINE save = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT B [ i ] = arr [ i ] NEW_LINE DEDENT B = sorted ( B ) NEW_LINE for i in range ( N - 1 , 1 , - 1 ) : NEW_LINE INDENT if ( B [ i ] != B [ i - 1 ] ) : NEW_LINE INDENT save = B [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT if ( save == - 1 ) : NEW_LINE INDENT print ( - 1 + " " ) NEW_LINE return NEW_LINE DEDENT if ( save == arr [ 0 ] and save != arr [ 1 ] ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT elif ( save == arr [ N - 1 ] and save != arr [ N - 2 ] ) : NEW_LINE INDENT print ( N ) NEW_LINE DEDENT for i in range ( 1 , N - 1 ) : NEW_LINE INDENT if ( save == arr [ i ] and ( save != arr [ i - 1 ] or save != arr [ i + 1 ] ) ) : NEW_LINE INDENT print ( i + 1 ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 3 , 4 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE printIndex ( arr , N ) NEW_LINE DEDENT