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 known 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++
#include<bits/stdc++.h>
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);
}
|
Java
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);
}
}
|
Python3
import sys
def minPeaks(list1):
n = len (list1)
result = []
for i in range (n):
min = sys.maxsize
index = - 1
size = len (list1)
for j in range (size):
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 ):
if (list1[j] > list1[j - 1 ] and
min > list1[j]):
min = list1[j]
index = j
elif (size = = 1 ):
min = list1[j]
index = j
elif (list1[j] > list1[j - 1 ] and
list1[j] > list1[j + 1 ] and
min > list1[j]):
min = list1[j]
index = j
list1.pop(index)
result.append( min )
print (result)
if __name__ = = "__main__" :
arr = [ 1 , 9 , 7 , 8 , 2 , 6 ]
minPeaks(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void minPeaks(List< int > list)
{
int n = list.Count;
List< int > result = new List< int >();
for ( int i = 0; i < n; i++)
{
int min = int .MaxValue;
int index = -1;
int size = list.Count;
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.RemoveAt(index);
result.Add(min);
}
for ( int i = 0; i < result.Count; i++)
Console.Write(result[i] + ", " );
}
public static void Main(String[] args)
{
List< int > arr = new List< int >{1, 9, 7,
8, 2, 6};
minPeaks(arr);
}
}
|
Javascript
<script>
function minPeaks(list)
{
var n = list.length;
var result = [];
for ( var i = 0; i < n; i++)
{
var min = 1000000000;
var index = -1;
var size = list.length;
for ( var 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.splice(index, 1);
result.push(min);
}
document.write( "[" );
for ( var i = 0; i < result.length; i++)
{
document.write( result[i] + ", " );
}
document.write( "]" );
}
var arr = [1, 9, 7, 8, 2, 6];
minPeaks(arr);
</script>
|
Output:
[6, 8, 9, 7, 2, 1]
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
01 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...