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 1Explanation:

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.

Below is the implementation of the above approach:

`// 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 Code ` `int` `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` |

*chevron_right*

*filter_none*

`// Java program for the above approach` `import` `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 Code` `public` `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` |

*chevron_right*

*filter_none*

`# 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()` |

*chevron_right*

*filter_none*

**Output:**

[5, 4, 3, 2, 1]

**Time Complexity:** *O(N*log N)* **Auxiliary Space:** *O(1)*

## Recommended Posts:

- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of array elements which are greater than all elements on its left
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Find the element before which all the elements are smaller than it, and after which all are greater
- Maximize the number of indices such that element is greater than element to its left
- Smallest subarray of size greater than K with sum greater than a given value
- Largest number not greater than N which can become prime after rearranging its digits
- Sum of all array elements less than X and greater than Y for Q queries
- Find maximum points which can be obtained by deleting elements from array
- Count of array elements which is smaller than both its adjacent elements
- Longest subarray in which all elements are greater than K
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Count the number of elements which are greater than any of element on right side of an array
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Merge first two minimum elements of the array until all the elements are greater than K
- Find all elements in array which have at-least two greater elements
- Find Array formed by adding each element of given array with largest element in new array to its left
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Permutation of a string with maximum number of characters greater than its adjacent characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

**Article Tags :**