For many applications in computer science, it is important to be able to manipulate an array in such a way that it becomes almost sorted. One such technique is to delete a minimum number of elements to get the desired result. This document will explain this technique and provide a guiding tutorial
The process of making an array almost sorted with a minimum number of deletions is to continuously delete elements in such a way that the number of deletions is minimized. This can be done by constructing a height array and then starting from the left of the array and deleting the tallest element each time.
The height array will contain the heights of each element in the original array, used to determine the ordering of elements. For example, if the original array is: [3, 4, 2, 5, 1], the corresponding height array would be [2, 4, 1, 5, 0].
- Construct a height array. This array should contain the heights of each element in the original array, used to determine the ordering of elements.
- Starting from the left of the array, look for the tallest element.
- Delete this element
- Compare the element with its left and its right element.
- If the deleted element is lower than its left element, proceed to the next element.
- If the deleted element is greater than its right element, repeat steps 2 - 4 on the remaining elements.
- Repeat steps 2 - 4 until the array is almost sorted.
What is an almost sorted array?
An almost sorted array is an array that is almost sorted but not completely sorted. This means that there might be some elements out of order but not enough to detract from the overall structure of the array.
What is a height array?
A height array is a numerical representation of the values of each element in the original array. It is used to determine the ordering of elements, where higher numbers come first.
Homa, Reza. "Minimum Number of Deletions in an Array to Make it Almost Sorted — Part I." Math Lover: Mathematics And Algorithm, 8 Oct. 2020, www.mathlover.org/2020/08/minimum-number-of-deletions-in-an-array.html.