Minimize prize count required such that smaller value gets less prize in an adjacent pair | Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to iterate over every elements of the array ; Loop to find the consecutive smaller elements at left ; Loop to find the consecutive smaller elements at right ; Driver Code
function findMinPrizes ( arr , n ) { let totalPrizes = 0 , j , x , y ; for ( let i = 0 ; i < n ; i ++ ) { x = 1 ; j = i ; while ( j > 0 && arr [ j ] > arr [ j - 1 ] ) { x ++ ; j -- ; } j = i ; y = 1 ; while ( j < n - 1 && arr [ j ] > arr [ j + 1 ] ) { y ++ ; j ++ ; } totalPrizes += Math . max ( x , y ) ; } document . write ( totalPrizes ) ; return 0 ; } let arr = [ 1 , 2 , 2 , 3 ] ; let n = arr . length ; findMinPrizes ( arr , n ) ;