Minimum peak elements from an array by their repeated removal at every iteration of the array | Java program for the above approach ; Function to return the list of minimum peak elements ; Length of original list ; Initialize resultant list ; Traverse each element of list ; Length of original list after removing the peak element ; Traverse new list after removal of previous min peak element ; Update min and index , if first element of list > next element ; Update min and index , if last element of list > previous one ; Update min and index , if list has single element ; Update min and index , if current element > adjacent elements ; Remove current min peak element from list ; Insert min peak into resultant list ; Print resultant list ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static void minPeaks ( ArrayList < Integer > list ) { int n = list . size ( ) ; ArrayList < Integer > result = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int min = Integer . MAX_VALUE ; int index = - 1 ; int size = list . size ( ) ; for ( int j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list . get ( j ) > list . get ( j + 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list . get ( j ) > list . get ( j - 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } else if ( size == 1 ) { min = list . get ( j ) ; index = j ; } else if ( list . get ( j ) > list . get ( j - 1 ) && list . get ( j ) > list . get ( j + 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } list . remove ( index ) ; result . add ( min ) ; } System . out . println ( result ) ; } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < > ( Arrays . asList ( 1 , 9 , 7 , 8 , 2 , 6 ) ) ; minPeaks ( arr ) ; } }