Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | Function to prlet the 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 ; Given array arr [ ] ; Length of array ; Function Call
function printIndex ( arr , N ) { let B = [ ] ; let save = - 1 ; for ( let i = 0 ; i < N ; i ++ ) { B [ i ] = arr [ i ] ; } B . sort ( ) ; for ( let i = N - 1 ; i >= 1 ; i -- ) { if ( B [ i ] != B [ i - 1 ] ) { save = B [ i ] ; break ; } } if ( save == - 1 ) { document . write ( - 1 + " " ) ; return ; } if ( save == arr [ 0 ] && save != arr [ 1 ] ) { document . write ( 1 ) ; } else if ( save == arr [ N - 1 ] && save != arr [ N - 2 ] ) { document . write ( N ) ; } for ( let i = 1 ; i < N - 1 ; i ++ ) { if ( save == arr [ i ] && ( save != arr [ i - 1 ] save != arr [ i + 1 ] ) ) { document . write ( i + 1 ) ; break ; } } } let arr = [ 5 , 3 , 4 , 4 , 5 ] ; let N = arr . length ; printIndex ( arr , N ) ;