Minimum peak elements from an array by their repeated removal at every iteration of the array | Python3 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 sys NEW_LINE def minPeaks ( list1 ) : NEW_LINE INDENT n = len ( list1 ) NEW_LINE result = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT min = sys . maxsize NEW_LINE index = - 1 NEW_LINE size = len ( list1 ) NEW_LINE for j in range ( size ) : NEW_LINE INDENT if ( j == 0 and j + 1 < size ) : NEW_LINE INDENT if ( list1 [ j ] > list1 [ j + 1 ] and min > list1 [ j ] ) : NEW_LINE INDENT min = list1 [ j ] ; NEW_LINE index = j ; NEW_LINE DEDENT DEDENT elif ( j == size - 1 and j - 1 >= 0 ) : NEW_LINE INDENT if ( list1 [ j ] > list1 [ j - 1 ] and min > list1 [ j ] ) : NEW_LINE INDENT min = list1 [ j ] NEW_LINE index = j NEW_LINE DEDENT DEDENT elif ( size == 1 ) : NEW_LINE INDENT min = list1 [ j ] NEW_LINE index = j NEW_LINE DEDENT elif ( list1 [ j ] > list1 [ j - 1 ] and list1 [ j ] > list1 [ j + 1 ] and min > list1 [ j ] ) : NEW_LINE INDENT min = list1 [ j ] NEW_LINE index = j NEW_LINE DEDENT DEDENT list1 . pop ( index ) NEW_LINE result . append ( min ) NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 9 , 7 , 8 , 2 , 6 ] NEW_LINE minPeaks ( arr ) NEW_LINE DEDENT