Minimize prize count required such that smaller value gets less prize in an adjacent pair | Java implementation to find the minimum prizes required such that adjacent smaller elements gets less number of prizes ; 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
import java . util . * ; class GFG { static int findMinPrizes ( int arr [ ] , int n ) { int totalPrizes = 0 , j , x , y ; for ( int 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 ) ; } System . out . print ( totalPrizes + "NEW_LINE"); return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = arr . length ; findMinPrizes ( arr , n ) ; } }