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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use the concept of Merge Sort.

1. Divide the input array into sub-arrays till the size of each sub-array becomes 1.
2. Start merging the element.
3. 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.
4. Repeat the above steps in each merging step such all elements with value smaller to it’s left have been deleted.
5. Finally print the resultant array.

Below is the implementation of the above approach:

 `# Python 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 ` ` `  `# Function defination for main() ` `def` `main(): ` ` `  `# Given array arr[] ` `    ``arr ``=` `[``5``, ``4``, ``3``, ``2``, ``1``] ` `    ``print``(mergeDel(arr)) ` ` `  `# Driver Code ` `main() `

Output:

```[5, 4, 3, 2, 1]
```

Time Complexity: O(N*log N)
Auxiliary Space: O(1) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.