Minimum peak elements from an array by their repeated removal at every iteration of the array
Given an array arr[] consisting of N distinct positive integers, the task is to repeatedly find the minimum peak element from the given array and remove that element until all the array elements are removed.
Peak Element: Any element in the array is know as the peak element based on the following conditions:
- If arr[i – 1] < arr[i] > arr[i + 1], where 1 < i < N – 1, then arr[i] is the peak element.
- If arr[0] > arr[1], then arr[0] is the peak element, where N is the size of the array.
- If arr[N – 2] < arr[N – 1], then arr[N – 1] is the peak element, where N is the size of the array.
If more than one peak element exists in the array, then the minimum value among them needs to be printed.
Examples:
Input: arr[] = {1, 9, 7, 8, 2, 6}
Output: [6, 8, 9, 7, 2, 1]
Explanation:
First min peak = 6, as 2 < 6.
The array after removing min peak will be [1, 9, 7, 8, 2].
Second min peak = 8, as 7 < 8 > 2.
The array after removing min peak will be [1, 9, 7, 2]
Third min peak = 9, as 1 < 9 > 7.
The array after removing min peak will be [1, 7, 2]
Fourth min peak = 7, as 1 < 7 > 2.
The array after removing min peak will be [1, 2]
Fifth min peak = 2, as 1 < 2.
The array after removing min peak will be [1]
Sixth min peak = 1.
Therefore, the list of minimum peak is [6, 8, 9, 7, 2, 1].Input: arr []= {1, 5, 3, 7, 2}
Output: [5, 7, 3, 2, 1]
Explanation:
First min peak = 5, as 1 < 5 > 3.
The array after removing min peak will be [1, 3, 7, 2]
Second min peak = 7, as 3 < 7 > 2.
The array after removing min peak will be [1, 3, 2]
Third min peak = 3, as 1 < 3 > 2.
The array after removing min peak will be [1, 2]
Fourth min peak = 2, as 1 < 2.
The array after removing min peak will be [1]
Fifth min peak = 1.
Therefore, the list of minimum peak is [5, 7, 3, 2, 1]
Approach: The idea is to find the minimum peak element of the array by iterating over the array using two nested loops, where the outer loop points to the current element and the inner loop execute to find the index of min peak element, remove that peak element from the array and store the current peak element in the resultant list. After completing the above steps, print all the minimum peak elements stored in the list.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; // Function to return the list of // minimum peak elements void minPeaks(vector< int >list) { // Length of original list int n = list.size(); // Initialize resultant list vector< int >result; // Traverse each element of list for ( int i = 0; i < n; i++) { int min = INT_MAX; int index = -1; // Length of original list after // removing the peak element int size = list.size(); // Traverse new list after removal // of previous min peak element for ( int j = 0; j < size; j++) { // Update min and index, // if first element of // list > next element 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) { // Update min and index, // if last element of // list > previous one if (list[j] > list[j - 1] && min > list[j]) { min = list[j]; index = j; } } // Update min and index, if // list has single element else if (size == 1) { min = list[j]; index = j; } // Update min and index, // if current element > // adjacent elements else if (list[j] > list[j - 1] && list[j] > list[j + 1] && min > list[j]) { min = list[j]; index = j; } } // Remove current min peak // element from list list.erase(list.begin() + index); // Insert min peak into // resultant list result.push_back(min); } // Print resultant list cout << "[" ; for ( int i = 0; i < result.size(); i++) { cout << result[i] << ", " ; } cout << "]" ; } // Driver Code int main() { // Given array arr[] vector< int > arr = { 1, 9, 7, 8, 2, 6 }; // Function call minPeaks(arr); } // This code is contributed by bikram2001jha |
Java
// Java program for the above approach import java.util.*; import java.lang.*; class GFG { // Function to return the list of // minimum peak elements static void minPeaks(ArrayList<Integer> list) { // Length of original list int n = list.size(); // Initialize resultant list ArrayList<Integer> result = new ArrayList<>(); // Traverse each element of list for ( int i = 0 ; i < n; i++) { int min = Integer.MAX_VALUE; int index = - 1 ; // Length of original list after // removing the peak element int size = list.size(); // Traverse new list after removal // of previous min peak element for ( int j = 0 ; j < size; j++) { // Update min and index, // if first element of // list > next element 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 ) { // Update min and index, // if last element of // list > previous one if (list.get(j) > list.get(j - 1 ) && min > list.get(j)) { min = list.get(j); index = j; } } // Update min and index, if // list has single element else if (size == 1 ) { min = list.get(j); index = j; } // Update min and index, // if current element > // adjacent elements 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; } } // Remove current min peak // element from list list.remove(index); // Insert min peak into // resultant list result.add(min); } // Print resultant list System.out.println(result); } // Driver Code public static void main(String[] args) { // Given array arr[] ArrayList<Integer> arr = new ArrayList<>( Arrays.asList( 1 , 9 , 7 , 8 , 2 , 6 )); // Function Call minPeaks(arr); } } |
Python3
# Python3 program for # the above approach import sys # Function to return the list of # minimum peak elements def minPeaks(list1): # Length of original list n = len (list1) # Initialize resultant list result = [] # Traverse each element of list for i in range (n): min = sys.maxsize index = - 1 # Length of original list # after removing the peak # element size = len (list1) # Traverse new list after removal # of previous min peak element for j in range (size): # Update min and index, # if first element of # list > next element if (j = = 0 and j + 1 < size): if (list1[j] > list1[j + 1 ] and min > list1[j]): min = list1[j]; index = j; elif (j = = size - 1 and j - 1 > = 0 ): # Update min and index, # if last element of # list > previous one if (list1[j] > list1[j - 1 ] and min > list1[j]): min = list1[j] index = j # Update min and index, if # list has single element elif (size = = 1 ): min = list1[j] index = j # Update min and index, # if current element > # adjacent elements elif (list1[j] > list1[j - 1 ] and list1[j] > list1[j + 1 ] and min > list1[j]): min = list1[j] index = j # Remove current min peak # element from list list1.pop(index) # Insert min peak into # resultant list result.append( min ) # Print resultant list print (result) # Driver Code if __name__ = = "__main__" : # Given array arr[] arr = [ 1 , 9 , 7 , 8 , 2 , 6 ] # Function call minPeaks(arr) # This code is contributed by Chitranayal |
C#
// C# program for // the above approach using System; using System.Collections.Generic; class GFG{ // Function to return the list of // minimum peak elements static void minPeaks(List< int > list) { // Length of original list int n = list.Count; // Initialize resultant list List< int > result = new List< int >(); // Traverse each element of list for ( int i = 0; i < n; i++) { int min = int .MaxValue; int index = -1; // Length of original list after // removing the peak element int size = list.Count; // Traverse new list after removal // of previous min peak element for ( int j = 0; j < size; j++) { // Update min and index, // if first element of // list > next element 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) { // Update min and index, // if last element of // list > previous one if (list[j] > list[j - 1] && min > list[j]) { min = list[j]; index = j; } } // Update min and index, if // list has single element else if (size == 1) { min = list[j]; index = j; } // Update min and index, // if current element > // adjacent elements else if (list[j] > list[j - 1] && list[j] > list[j + 1] && min > list[j]) { min = list[j]; index = j; } } // Remove current min peak // element from list list.RemoveAt(index); // Insert min peak into // resultant list result.Add(min); } // Print resultant list for ( int i = 0; i < result.Count; i++) Console.Write(result[i] + ", " ); } // Driver Code public static void Main(String[] args) { // Given array []arr List< int > arr = new List< int >{1, 9, 7, 8, 2, 6}; // Function Call minPeaks(arr); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program for the above approach // Function to return the list of // minimum peak elements function minPeaks(list) { // Length of original list var n = list.length; // Initialize resultant list var result = []; // Traverse each element of list for ( var i = 0; i < n; i++) { var min = 1000000000; var index = -1; // Length of original list after // removing the peak element var size = list.length; // Traverse new list after removal // of previous min peak element for ( var j = 0; j < size; j++) { // Update min and index, // if first element of // list > next element 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) { // Update min and index, // if last element of // list > previous one if (list[j] > list[j - 1] && min > list[j]) { min = list[j]; index = j; } } // Update min and index, if // list has single element else if (size == 1) { min = list[j]; index = j; } // Update min and index, // if current element > // adjacent elements else if (list[j] > list[j - 1] && list[j] > list[j + 1] && min > list[j]) { min = list[j]; index = j; } } // Remove current min peak // element from list list.splice(index, 1); // Insert min peak into // resultant list result.push(min); } // Print resultant list document.write( "[" ); for ( var i = 0; i < result.length; i++) { document.write( result[i] + ", " ); } document.write( "]" ); } // Driver Code // Given array arr[] var arr = [1, 9, 7, 8, 2, 6]; // Function call minPeaks(arr); // This code is contributed by itsok. </script> |
[6, 8, 9, 7, 2, 1]
Time Complexity: O(N2)
Auxiliary Space: O(N)