Minimum peak elements from an array by their repeated removal at every iteration of the array | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minPeaks ( vector < int > list ) { int n = list . size ( ) ; vector < int > result ; for ( int i = 0 ; i < n ; i ++ ) { int min = INT_MAX ; int index = -1 ; int size = list . size ( ) ; for ( int j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list [ j ] > list [ j - 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( size == 1 ) { min = list [ j ] ; index = j ; } else if ( list [ j ] > list [ j - 1 ] && list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } list . erase ( list . begin ( ) + index ) ; result . push_back ( min ) ; } cout << " [ " ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { cout << result [ i ] << " , ▁ " ; } cout << " ] " ; } int main ( ) { vector < int > arr = { 1 , 9 , 7 , 8 , 2 , 6 } ; minPeaks ( arr ) ; }