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:

// 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 elemnt 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); 
    } 
Output:
[6, 8, 9, 7, 2, 1]
Time Complexity: O(N2)
Auxiliary Space: O(N)

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation