Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
Last Updated :
14 Sep, 2023
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 ai – 1 > ai < ai + 1 or ai – 1 < ai > ai + 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(n2):
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 ai such that
ai-1 > ai > ai+1 or
ai-1 < ai < ai+1
So, count all those ai.
How does this work?
Let’s define ai be as,
Peak if 0 < i < n – 1 and ai – 1 < ai < ai + 1.
Valley if 0 < i < n – 1 and ai – 1 > ai > ai + 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 ai which is neither a peak or valley:
- If ai is an endpoint, then clearly no peak or valley can be formed.
- If ai – 1 < ai < ai + 1, then after removal of ai, the element ai – 1 and ai + 1 will become adjacent. But since, ai – 1 < ai + 1, the status of ai – 1 or ai + 1 will not change i.e if ai – 1 or ai + 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 ai – 1 > ai > ai + 1.
2. Suppose we remove a peak or valley:
- Suppose we remove a peak, so ai – 1 < ai > ai + 1. Now it’s possible that ai – 1 or ai + 1could become a peak or valley. But we can show that they can’t both change, because either ai – 1 < ai + 1, which means ai – 1 status never change, or ai – 1 > ai + 1, which means ai + 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 ai – 1 > ai < ai + 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumDeletions( int a[],
int n)
{
if (n <= 2)
return 0;
int count = 0;
for ( int i = 0; i < n - 2; i++)
{
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;
}
int main()
{
int a[] = { 5, 2, 3, 6, 1 };
int n = sizeof (a) / sizeof (a[0]);
cout << minimumDeletions(a, n)
<< endl;
return 0;
}
|
Java
class GFG
{
static int minimumDeletions( int a[],
int n)
{
if (n <= 2 )
return 0 ;
int count = 0 ;
for ( int i = 0 ; i < n - 2 ; i++)
{
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;
}
public static void main (String[] args)
{
int a[] = { 5 , 2 , 3 , 6 , 1 };
int n = a.length;
System.out.println(minimumDeletions(a, n));
}
}
|
Python3
def minimumDeletions(a, n):
if (n < = 2 ):
return 0
count = 0
for i in range (n - 2 ):
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
a = [ 5 , 2 , 3 , 6 , 1 ]
n = len (a)
print (minimumDeletions(a, n))
|
C#
using System;
class GFG
{
static int minimumDeletions( int []a,
int n)
{
if (n <= 2)
return 0;
int count = 0;
for ( int i = 0; i < n - 2; i++)
{
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;
}
public static void Main ()
{
int []a = { 5, 2, 3, 6, 1 };
int n = a.Length;
Console.Write(minimumDeletions(a, n));
}
}
|
PHP
<?php
function minimumDeletions( $a , $n )
{
if ( $n <= 2)
return 0;
$count = 0;
for ( $i = 0; $i < $n - 2; $i ++)
{
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 ;
}
{
$a = array ( 5, 2, 3, 6, 1 );
$n = sizeof( $a ) / sizeof( $a [0]);
echo minimumDeletions( $a , $n ) ;
return 0;
}
?>
|
Javascript
<script>
function minimumDeletions(a, n) {
if (n <= 2)
return 0;
let count = 0;
for (let i = 0; i < n - 2; i++) {
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;
}
let a = [5, 2, 3, 6, 1];
let n = a.length;
document.write(minimumDeletions(a, n) + "<br>" );
</script>
|
Time Complexity : O(n).
Share your thoughts in the comments
Please Login to comment...