Given an array of **n** distinct positive integers. The task is to find the minimum number of elements to be removed such that no three consecutive elements in the array are either increasing or decreasing. That is, after removal either a_{i – 1} > a_{i} < a_{i + 1} or a_{i – 1} < a_{i} > a_{i + 1}.

**Examples :**

Input : arr[] = {5, 2, 3, 6, 1} Output : 1 Given arr[] is not in required form (2 < 3 < 6). So, after removal of 6 or 3, array will be in required manner. Input : arr[] = { 4, 2, 6, 3, 10, 1} Output : 0

Observe, for n < 3, output will be 0 since no element need to be remove.

**Method 1 O(n ^{2}):**

If we take closer look, we can notice that after the removal of element from arr, what remain is zigzag subsequence of arr. Suppose new array is arr’. Then the number of deletion we performed to arrive arr’ is simply size of arr – size of arr’. Our goal is to minimize this difference. Note size of arr is fixed, so we must minimize – size of arr’ or maximize the size of arr’. Thus the problem is reduce to finding Longest Zig-Zag Subsequence.

**Method 2 O(n): (tricky)**

The elements to be removed are those a_{i} such that

a_{i-1} > a_{i} > a_{i+1} or

a_{i-1} < a_{i} < a_{i+1}

So, count all those a_{i}.

**How does this work?**

Let’s define a_{i} be as,

Peak if 0 < i < n – 1 and a_{i – 1} < a_{i} < a_{i + 1}.

Valley if 0 < i < n – 1 and a_{i – 1} > a_{i} > a_{i + 1}.

Let P(arr) and V(arr) be the number of peak and number of valleys respectively.

Observe, in any zigzag array, all the element except the endpoints are either peak or valley i.e n = P(arr) + V(arr) + 2.

Now, we can show that any deletion will never increase the total number of peaks and valleys in any array:

1. Suppose we remove an element a_{i} which is neither a peak or valley:

- If a
_{i}is an endpoint, then clearly no peak or valley can be formed. - If a
_{i – 1}< a_{i}< a_{i + 1}, then after removal of a_{i}, the element a_{i – 1}and a_{i + 1}will become adjacent. But since, a_{i – 1}< a_{i + 1}, the status of a_{i – 1}or a_{i + 1}will not change i.e if a_{i – 1}or a_{i + 1}was originally a peak/valley/neither then it will stay a peak/valley/neither after the removal since their comparison with their neighbor will be same. In particular, no new peak or valley is formed. - Similar is the case with a
_{i – 1}> a_{i}> a_{i + 1}.

2. Suppose we remove a peak or valley:

- Suppose we remove a peak, so a
_{i – 1}< a_{i}> a_{i + 1}. Now it’s possible that a_{i – 1}or a_{i + 1}could become a peak or valley. But we can show that they can’t both change, because either a_{i – 1}< a_{i + 1}, which means a_{i – 1}status never change, or a_{i – 1}> a_{i + 1}, which means a_{i + 1}status never change. Therefore atmost one peak or valley will be formed, but since we removed a peak, the total number of peaks/valley will not increase. - The same thing happen when we remove a valley i.e a
_{i – 1}> a_{i}< a_{i + 1}.

Thus any deletion will not change P(arr) + V(arr).

Using this argument, we can say the longest zigzag subsequence of arr has a length at most P(arr) + V(arr) + 2.

On the other hand, arr has a zigzag subsequence of length P(arr) + V(arr) + 2, the sequence of peaks, valley, endpoints of arr. It can be shown that this subsequence is zigzag (by trying some example), hence the longest zigzag subsequence has a length of atleast P(arr) + V(arr) + 2. So, from previous two argument we can say that longest zigzag subsequence has exact length of P(arr) + V(arr) + 2.

Thus the answer to our problem is size of arr – P(arr) – V(arr) – 2.

**Algorithm :**

1. Initialize count = 0. 2. For each element arr[i] from index i = 1 to n - 2. (i) if (arr[i-1] arr[i+1]) (ii) increment count by 1. 3. return count.

`// C++ program to find minimum ` `// elements to be removed so ` `// that array becomes zig-zag. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `int` `minimumDeletions(` `int` `a[], `
` ` `int` `n) `
`{ ` ` ` `if` `(n <= 2) `
` ` `return` `0; `
` ` ` ` `// If number of element `
` ` `// is greater than 2. `
` ` `int` `count = 0; `
` ` `for` `(` `int` `i = 0; i < n - 2; i++) `
` ` `{ `
` ` `// If three element are `
` ` `// consecutively increasing `
` ` `// or decreasing. `
` ` `if` `((a[i] < a[i + 1] && `
` ` `a[i + 1] < a[i + 2]) || `
` ` `(a[i] > a[i + 1] && `
` ` `a[i + 1] > a[i + 2])) `
` ` `count++; `
` ` `} `
` ` ` ` `return` `count; `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `int` `a[] = { 5, 2, 3, 6, 1 }; `
` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); `
` ` ` ` `cout << minimumDeletions(a, n) `
` ` `<< endl; `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java program to find minimum ` `// elements to be removed so that ` `// array becomes zig-zag. ` ` ` `class` `GFG `
`{ ` ` ` `static` `int` `minimumDeletions(` `int` `a[], `
` ` `int` `n) `
` ` `{ `
` ` `if` `(n <= ` `2` `) `
` ` `return` `0` `; `
` ` ` ` `// If number of element `
` ` `// is greater than 2. `
` ` `int` `count = ` `0` `; `
` ` `for` `(` `int` `i = ` `0` `; i < n - ` `2` `; i++) `
` ` `{ `
` ` `// If three element are `
` ` `// consecutively increasing `
` ` `// or decreasing. `
` ` `if` `((a[i] < a[i + ` `1` `] && `
` ` `a[i + ` `1` `] < a[i + ` `2` `]) || `
` ` `(a[i] > a[i + ` `1` `] && `
` ` `a[i + ` `1` `] > a[i + ` `2` `])) `
` ` `count++; `
` ` `} `
` ` ` ` `return` `count; `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `main (String[] args) `
` ` `{ `
` ` `int` `a[] = { ` `5` `, ` `2` `, ` `3` `, ` `6` `, ` `1` `}; `
` ` `int` `n = a.length; `
` ` ` ` `System.out.println(minimumDeletions(a, n)); `
` ` `} `
`} ` |

*chevron_right*

*filter_none*

`# Python3 program to find minimum ` `# elements to be removed so that ` `# array becomes zig-zag. ` ` ` `def` `minimumDeletions(a, n): `
` ` ` ` `if` `(n <` `=` `2` `): `
` ` `return` `0`
` ` ` ` `# If number of element is `
` ` `# greater than 2. `
` ` `count ` `=` `0`
` ` `for` `i ` `in` `range` `(n ` `-` `2` `): `
` ` ` ` `# If three element are `
` ` `# consecutively increasing `
` ` `# or decreasing. `
` ` `if` `((a[i] < a[i ` `+` `1` `] ` `and`
` ` `a[i ` `+` `1` `] < a[i ` `+` `2` `]) ` `or`
` ` `(a[i] > a[i ` `+` `1` `] ` `and`
` ` `a[i ` `+` `1` `] > a[i ` `+` `2` `])): `
` ` `count ` `+` `=` `1`
` ` ` ` `return` `count `
` ` `# Driver Code ` `a ` `=` `[ ` `5` `, ` `2` `, ` `3` `, ` `6` `, ` `1` `] `
`n ` `=` `len` `(a) `
`print` `(minimumDeletions(a, n)) `
` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

`// C# program to find minimum ` `// elements to be removed so ` `// that array becomes zig-zag. ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `static` `int` `minimumDeletions(` `int` `[]a, `
` ` `int` `n) `
` ` `{ `
` ` `if` `(n <= 2) `
` ` `return` `0; `
` ` ` ` `// If number of element is `
` ` `// greater than 2. `
` ` `int` `count = 0; `
` ` `for` `(` `int` `i = 0; i < n - 2; i++) `
` ` `{ `
` ` `// If three element are `
` ` `// consecutively increasing `
` ` `// or decreasing. `
` ` `if` `((a[i] < a[i + 1] && `
` ` `a[i + 1] < a[i + 2]) || `
` ` `(a[i] > a[i + 1] && `
` ` `a[i + 1] > a[i + 2])) `
` ` `count++; `
` ` `} `
` ` ` ` `return` `count; `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `Main () `
` ` `{ `
` ` `int` `[]a = { 5, 2, 3, 6, 1 }; `
` ` `int` `n = a.Length; `
` ` ` ` `Console.Write(minimumDeletions(a, n)); `
` ` `} `
`} ` ` ` `// This code is contributed ` `// by nitin mittal ` |

*chevron_right*

*filter_none*

`<?php ` `// PHP program to find minimum ` `// elements to be removed so that ` `// array becomes zig-zag. ` ` ` `function` `minimumDeletions(` `$a` `, ` `$n` `) `
`{ ` ` ` `if` `(` `$n` `<= 2) `
` ` `return` `0; `
` ` ` ` `// If number of element `
` ` `// is greater than 2. `
` ` `$count` `= 0; `
` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 2; ` `$i` `++) `
` ` `{ `
` ` `// If three element are `
` ` `// consecutively increasing `
` ` `// or decreasing. `
` ` `if` `((` `$a` `[` `$i` `] < ` `$a` `[` `$i` `+ 1] && `
` ` `$a` `[` `$i` `+ 1] < ` `$a` `[` `$i` `+ 2]) || `
` ` `(` `$a` `[` `$i` `] > ` `$a` `[` `$i` `+ 1] && `
` ` `$a` `[` `$i` `+ 1] > ` `$a` `[` `$i` `+ 2])) `
` ` `$count` `++; `
` ` `} `
` ` ` ` `return` `$count` `; `
`} ` ` ` `// Driver Code ` ` ` `{ ` ` ` `$a` `= ` `array` `( 5, 2, 3, 6, 1 ); `
` ` `$n` `= sizeof(` `$a` `) / sizeof(` `$a` `[0]); `
` ` ` ` `echo` `minimumDeletions(` `$a` `, ` `$n` `) ; `
` ` ` ` `return` `0; `
`} ` ` ` `// This code is contributed ` `// by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

1

**Time Complexity : **O(n).

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Maximize length of longest non-decreasing array possible by the elements present in either ends of the given array
- Remove minimum elements from either side such that 2*min becomes more than max
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Minimum sum possible by assigning every increasing/decreasing consecutive pair with values in that order
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Find minimum sum such that one of every three consecutive elements is taken
- Find three element from different three arrays such that a + b + c = sum
- Find three element from given three arrays such that their sum is X | Set 2
- Find the maximum element in an array which is first increasing and then decreasing
- Sum of array elements that is first continuously increasing then decreasing
- Split the array elements into strictly increasing and decreasing sequence
- Check if it is possible to make array increasing or decreasing by rotating the array
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Divide array into increasing and decreasing subsequence without changing the order
- Check if an array is increasing or decreasing
- Sort an Increasing-Decreasing Array
- Check if Array forms an increasing-decreasing sequence or vice versa