Question: Given an array numbers = {2, 7, 8, 5, 1, 6, 3, 9, 4}. Check $$a := [2, 7, 8, 5, 1, 6, 3, 9, 4]$$ Check the below conditions, both the conditions should be satisfied.
a[i] > a[i-1] or if first element a[i] > a[i+1] a[i] > a[i+1] or if last element a[lastelement] > a[lastelement - 1] 1st Iteration - 8, 6, 9 are peak values. Remove the smallest ele. Remove 6. New arr {2, 7, 8, 5, 1, 3, 9, 4}. Output Arr - {6}
2nd Iteration - 8, 9 are peak values. Remove the smallest ele. Remove 8. New arr {2, 7, 5, 1, 3, 9, 4}. Output Arr - {6, 8}
3rd Iteration - 7, 9 are peak values. Remove the smallest ele. Remove 7. New arr {2, 5, 1, 3, 9, 4}. Output Arr - {6, 7, 8}
4th Iteration - 5, 9 are peak values. Remove the smallest ele. Remove 5. New arr {2, 1, 3, 9, 4}. Output Arr - {6, 7, 8, 5}
5th Iteration - 2, 9 are peak values. Remove the smallest ele. Remove 2. New arr {1, 3, 9, 4}. Output Arr - {6, 7, 8, 5, 2}
6th Iteration - 9 are peak values. Remove the smallest ele. Remove 9. New arr {1, 3, 4}. Output Arr - {6, 7, 8, 5, 2, 9}
7th Iteration - 4 are peak values. Remove the smallest ele. Remove 4. New arr {1, 3}. Output Arr - {6, 7, 8, 5, 2, 9, 4}\begin{gather} \text{If }a[i] > a[i-1]\text{ or if first element }a[i] > a[i+1] \end{gather}
8th Iteration - 3 are peak values. Remove the smallest ele. Remove 3. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3}\begin{gather} \text{If }a[i] > a[i+1]\text{ or if last element }a[LastIndex] > a[LastIndex - 1] \end{gather}
9th Iteration - 1 are peak values. Remove the smallest ele. Remove 1. New arr {1}. Output Arr - {6, 7, 8, 5, 2, 9, 4, 3, 1}
1st Iteration - 8, 6, 9 are peak values.
- Remove the smallest ele.
- Remove 6.
- New arr
{2, 7, 8, 5, 1, 3, 9, 4}. - Output Arr -
{6}
2nd Iteration - 8, 9 are peak values.
- Remove the smallest ele.
- Remove 8.
- New arr
{2, 7, 5, 1, 3, 9, 4}. - Output Arr -
{6, 8}
3rd Iteration - 7, 9 are peak values.
- Remove the smallest ele.
- Remove 7. New arr
{2, 5, 1, 3, 9, 4}. - Output Arr - {6, 7, 8}
4th Iteration - 5, 9 are peak values.
- Remove the smallest ele.
- Remove 5.
- New arr
{2, 1, 3, 9, 4}. - Output Arr -
{6, 7, 8, 5}
5th Iteration - 2, 9 are peak values.
- Remove the smallest ele.
- Remove 2.
- New arr
{1, 3, 9, 4}. - Output Arr -
{6, 7, 8, 5, 2}
6th Iteration - 9 are peak values.
- Remove the smallest ele.
- Remove 9.
- New arr
{1, 3, 4}. - Output Arr -
{6, 7, 8, 5, 2, 9}
7th Iteration - 4 are peak values.
- Remove the smallest ele.
- Remove 4.
- New arr
{1, 3}. - Output Arr -
{6, 7, 8, 5, 2, 9, 4}
8th Iteration - 3 are peak values.
- Remove the smallest ele.
- Remove 3.
- New arr {1}.
- Output Arr -
{6, 7, 8, 5, 2, 9, 4, 3}
9th Iteration - 1 are peak values.
- Remove the smallest ele.
- Remove 1.
- New arr {1}.
- Output Arr -
{6, 7, 8, 5, 2, 9, 4, 3, 1}
Output: {6, 8, 7, 5, 2, 9, 4, 3, 1}{6, 8, 7, 5, 2, 9, 4, 3, 1}