Reduce the array by deleting elements which are greater than all elements to its left

09 Jun, 2021
Given an array arr[] of N integers, the task is to delete the element from the given array if element to it’s left is smaller than it. Keep on deleting the elements from the array until no element has a smaller adjacent left element. Print the resultant array after above operation.
Examples: 
Input: arr[] = {2, 4, 1, 3, 4} Output: 2 1 Explanation: Since 4 is greater than 2 remove 4, and arr become {2, 1, 3, 4}. Now 3 is greater than 1 so remove 3 and arr become {2, 1, 4}. Now 4 is greater than 1 so remove 4 and arr become {2, 1}. Now no elements satisfy the removing criteria so the resultant array is {2, 1}.
Input: arr[] = {5, 4, 3, 2, 1} Output: 5 4 3 2 1 
Approach: The idea is to use the concept of Merge Sort.
Divide the input array into sub-arrays till the size of each sub-array becomes 1.Start merging the element.While merging, delete elements from the left subarray till it’s rightmost element, which have a value greater than the leftmost element of the right subarray.Repeat the above steps in each merging step such all elements with value smaller to it’s left have been deleted.Finally print the resultant array.
Divide the input array into sub-arrays till the size of each sub-array becomes 1.
Start merging the element.
While merging, delete elements from the left subarray till it’s rightmost element, which have a value greater than the leftmost element of the right subarray.
Repeat the above steps in each merging step such all elements with value smaller to it’s left have been deleted.
Finally print the resultant array.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include<bits/stdc++.h>using namespace std; // Function to implement merging of arr[]vector<int> merge(vector<int> x, vector<int> y){    for(auto i : y)    {        if (x[x.size() - 1] > i)            x.push_back(i);    }    return x;} // Function to delete all elements which// satisfy the condition A[i] > A[i-1]vector<int> mergeDel(vector<int> l){         // Divide array into its subarray    if (l.size() == 1)        return l;     int m = l.size() / 2;         vector<int> temp1 = {l.begin() + 0,                         l.begin() + m};    vector<int> temp2 = {l.begin() + m,                         l.end()};         // Getting back merged array with all     // its right element greater than    // left one.    return merge(mergeDel(temp1),                 mergeDel(temp2));} // Driver Codeint main(){         // Given array arr[]    vector<int> arr({ 5, 4, 3, 2, 1 });    vector<int> ans = mergeDel(arr);         cout << "[ ";    for(auto x: ans)        cout << x << ", ";             cout << "]";} // This code is contributed by SURENDRA_GANGWAR
// Java program for the above approachimport java.util.ArrayList;import java.util.Arrays; class GFG{     // Function to implement merging of arr[]static ArrayList<Integer> merge(ArrayList<Integer> x,                                ArrayList<Integer> y){    for(Integer i : y)    {        if (x.get(x.size() - 1) > i)            x.add(i);    }    return x;} // Function to delete all elements which// satisfy the condition A[i] > A[i-1]static ArrayList<Integer> mergeDel(ArrayList<Integer> l){         // Divide array into its subarray    if (l.size() == 1)        return l;     int m = l.size() / 2;     ArrayList<Integer> temp1 = new ArrayList<>(        l.subList(0, m));    ArrayList<Integer> temp2 = new ArrayList<>(        l.subList(m, l.size()));     // Getting back merged array with all    // its right element greater than    // left one.    return merge(mergeDel(temp1), mergeDel(temp2));} // Driver Codepublic static void main(String[] args){         // Given array arr[]    Integer[] ar = { 5, 4, 3, 2, 1 };         ArrayList<Integer> arr = new ArrayList<>(        Arrays.asList(ar));    ArrayList<Integer> ans = mergeDel(arr);         System.out.print("[ ");    for(Integer x : ans)        System.out.print(x + ", ");             System.out.println("]");}} // This code is contributed by sanjeev2552
# Python3 program for the above approach # Function to delete all elements which# satisfy the condition A[i] > A[i-1]def mergeDel(l):     # Divide array into its subarray    if len(l) == 1:         return l     m = int( len(l) / 2)     # Getting back merged array with all    # its right element greater than left one.    return merge(mergeDel(l[ 0 : m ]),                 mergeDel(l[ m : len(l)]) )  # Function to implement merging of arr[]def merge(x, y):     for i in y:         if x[-1] > i :             x = x + [i]     return x   # Driver Code # Function defination for main()def main(): # Given array arr[]    arr = [5, 4, 3, 2, 1]    print(mergeDel(arr)) main()
// C# program for the above approachusing System;using System.Collections.Generic; class GFG{ // Function to implement merging of arr[]static List<int> merge(List<int> x, List<int> y){    foreach(int i in y)    {        if (x[x.Count - 1] > i)            x.Add(i);    }    return x;} // Function to delete all elements which// satisfy the condition A[i] > A[i-1]static List<int> mergeDel(List<int> l){         // Divide array into its subarray    if (l.Count == 1)        return l;     int m = l.Count / 2;     List<int> temp1 = l.GetRange(0, m);    List<int> temp2 = l.GetRange(m, l.Count - m);     // Getting back merged array with all    // its right element greater than    // left one.    return merge(mergeDel(temp1),                 mergeDel(temp2));} // Driver Codepublic static void Main(string[] args){         // Given array arr[]    List<int> arr = new List<int>{ 5, 4, 3, 2, 1 };     List<int> ans = mergeDel(arr);     Console.Write("[ ");    foreach(int x in ans)        Console.Write(x + ", ");     Console.Write("]");}} // This code is contributed by chitranayal
<script>// Javascript program for the above approach // Function to implement merging of arr[]function  merge(x,y){    for(let i=0;i<y.length;i++)    {        if (x[x.length - 1] > y[i])            x.push(y[i]);    }    return x;} // Function to delete all elements which// satisfy the condition A[i] > A[i-1]function mergeDel(l){    // Divide array into its subarray    if (l.length == 1)        return l;      let m = Math.floor(l.length / 2);      let temp1 = l.slice(0, m);    let temp2 = l.slice(m, l.length);      // Getting back merged array with all    // its right element greater than    // left one.    return merge(mergeDel(temp1), mergeDel(temp2));}// Driver Code // Given array arr[]let arr=[5, 4, 3, 2, 1];let ans = mergeDel(arr);document.write("[ ");document.write(ans.join(", "));document.write("]<br>");  // This code is contributed by avanitrachhadiya2155</script>
[5, 4, 3, 2, 1]
 
Time Complexity: O(N*log N) Auxiliary Space: O(1) 
SURENDRA_GANGWAR
sanjeev2552
ukasp
avanitrachhadiya2155
Arrays
Competitive Programming
Divide and Conquer
Arrays
Divide and Conquer
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
Chocolate Distribution Problem
What is Data Structure: Types, Classifications and Applications
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Modulo 10^9+7 (1000000007)
Prefix Sum Array - Implementation and Applications in Competitive Programming