# Remove minimum elements from array such that no three consecutive element are either increasing or decreasing

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++

`// 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;` `}` |

## Java

`// 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));` ` ` `}` `}` |

## Python3

`# 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.` |

## C#

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

## PHP

`<?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.` `?>` |

## Javascript

`<script>` `// JavaScript 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.` ` ` `let count = 0;` ` ` `for` `(let 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` `let a = [5, 2, 3, 6, 1];` `let n = a.length;` `document.write(minimumDeletions(a, n) + ` `"<br>"` `);` `</script>` |

**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 write.geeksforgeeks.org or mail your article to review-team@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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.