Minimum removals required to make a given array Bitonic
Given an array arr[] of size N, the task is to find the minimum number of array elements required to be removed from the array such that the given array is converted to a bitonic array.
Examples:
Input: arr[] = { 2, 1, 1, 5, 6, 2, 3, 1 }
Output: 3
Explanation:
Removing arr[0], arr[1] and arr[5] modifies arr[] to { 1, 5, 6, 3, 1 }
Since the array elements follow an increasing order followed by a decreasing order, the required output is 3.
Input: arr[] = { 1, 3, 1 }
Output: 0
Explanation:
The given array is already a bitonic array. Therefore, the required output is 3.
Approach: The problem can be solved based on the concept of the longest increasing subsequence problem. Follow the steps below to solve the problem:
- Initialize a variable, say left[], such that left[i] stores the length of the longest increasing subsequence up to the ith index.
- Initialize a variable, say right[], such that right[i] stores the length of the longest decreasing subsequence over the range [i, N].
- Traverse left[] and right[] array using variable i and find the maximum value of (left[i] + right[i] – 1) and store it in a variable, say maxLen.
- Finally, print the value of N – maxLen.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void min_element_removal( int arr[], int N)
{
vector< int > left(N, 1);
vector< int > right(N, 1);
for ( int i = 1; i < N; i++) {
for ( int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
left[i] = max(left[i],
left[j] + 1);
}
}
}
for ( int i = N - 2; i >= 0;
i--) {
for ( int j = N - 1; j > i;
j--) {
if (arr[i] > arr[j]) {
right[i] = max(right[i],
right[j] + 1);
}
}
}
int maxLen = 0;
for ( int i = 1; i < N - 1; i++) {
maxLen = max(maxLen, left[i] + right[i] - 1);
}
cout << (N - maxLen) << "\n" ;
}
void makeBitonic( int arr[], int N)
{
if (N == 1) {
cout << "0" << endl;
return ;
}
if (N == 2) {
if (arr[0] != arr[1])
cout << "0" << endl;
else
cout << "1" << endl;
return ;
}
min_element_removal(arr, N);
}
int main()
{
int arr[] = { 2, 1, 1, 5, 6, 2, 3, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
makeBitonic(arr, N);
return 0;
}
|
C
#include <stdio.h>
#define max(a,b) ((a) > (b) ? (a) : (b)) //defining max
void min_element_removal( int arr[], int N)
{
int left[N];
for ( int i = 0; i < N; i++)
left[i] = 1;
int right[N];
for ( int i = 0; i < N; i++)
right[i] = 1;
for ( int i = 1; i < N; i++) {
for ( int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
left[i] = max(left[i], left[j] + 1);
}
}
}
for ( int i = N - 2; i >= 0;
i--) {
for ( int j = N - 1; j > i;
j--) {
if (arr[i] > arr[j]) {
right[i] = max(right[i], right[j] + 1);
}
}
}
int maxLen = 0;
for ( int i = 1; i < N - 1; i++) {
maxLen = max(maxLen, left[i] + right[i] - 1);
}
printf ( "%d\n" , (N - maxLen));
}
void makeBitonic( int arr[], int N)
{
if (N == 1) {
printf ( "0\n" );
return ;
}
if (N == 2) {
if (arr[0] != arr[1])
printf ( "0\n" );
else
printf ( "1\n" );
return ;
}
min_element_removal(arr, N);
}
int main()
{
int arr[] = { 2, 1, 1, 5, 6, 2, 3, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
makeBitonic(arr, N);
return 0;
}
|
Java
class GFG {
static void min_element_removal( int arr[], int N)
{
int left[] = new int [N];
for ( int i = 0 ; i < N; i++)
left[i] = 1 ;
int right[] = new int [N];
for ( int i = 0 ; i < N; i++)
right[i] = 1 ;
for ( int i = 1 ; i < N; i++) {
for ( int j = 0 ; j < i; j++) {
if (arr[j] < arr[i]) {
left[i] = Math.max(left[i],
left[j] + 1 );
}
}
}
for ( int i = N - 2 ; i >= 0 ;
i--) {
for ( int j = N - 1 ; j > i;
j--) {
if (arr[i] > arr[j]) {
right[i] = Math.max(right[i],
right[j] + 1 );
}
}
}
int maxLen = 0 ;
for ( int i = 1 ; i < N - 1 ; i++) {
maxLen = Math.max(maxLen, left[i] + right[i] - 1 );
}
System.out.println(N - maxLen);
}
static void makeBitonic( int arr[], int N)
{
if (N == 1 ) {
System.out.println( "0" );
return ;
}
if (N == 2 ) {
if (arr[ 0 ] != arr[ 1 ])
System.out.println( "0" );
else
System.out.println( "1" );
return ;
}
min_element_removal(arr, N);
}
public static void main (String[] args) {
int arr[] = { 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 };
int N = arr.length;
makeBitonic(arr, N);
}
}
|
Python3
def min_element_removal(arr, N):
left = [ 1 ] * N
right = [ 1 ] * (N)
for i in range ( 1 , N):
for j in range (i):
if (arr[j] < arr[i]):
left[i] = max (left[i], left[j] + 1 )
for i in range (N - 2 , - 1 , - 1 ):
for j in range (N - 1 , i, - 1 ):
if (arr[i] > arr[j]):
right[i] = max (right[i], right[j] + 1 )
maxLen = 0
for i in range ( 1 , N - 1 ):
maxLen = max (maxLen, left[i] + right[i] - 1 )
print ((N - maxLen))
def makeBitonic(arr, N):
if (N = = 1 ):
print ( "0" )
return
if (N = = 2 ):
if (arr[ 0 ] ! = arr[ 1 ]):
print ( "0" )
else :
print ( "1" )
return
min_element_removal(arr, N)
if __name__ = = '__main__' :
arr = [ 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 ]
N = len (arr)
makeBitonic(arr, N)
|
C#
using System;
class GFG{
static void min_element_removal( int []arr, int N)
{
int []left = new int [N];
for ( int i = 0; i < N; i++)
left[i] = 1;
int []right = new int [N];
for ( int i = 0; i < N; i++)
right[i] = 1;
for ( int i = 1; i < N; i++)
{
for ( int j = 0; j < i; j++)
{
if (arr[j] < arr[i])
{
left[i] = Math.Max(left[i],
left[j] + 1);
}
}
}
for ( int i = N - 2; i >= 0; i--)
{
for ( int j = N - 1; j > i; j--)
{
if (arr[i] > arr[j])
{
right[i] = Math.Max(right[i],
right[j] + 1);
}
}
}
int maxLen = 0;
for ( int i = 1; i < N - 1; i++)
{
maxLen = Math.Max(maxLen, left[i] +
right[i] - 1);
}
Console.WriteLine(N - maxLen);
}
static void makeBitonic( int []arr, int N)
{
if (N == 1)
{
Console.WriteLine( "0" );
return ;
}
if (N == 2)
{
if (arr[0] != arr[1])
Console.WriteLine( "0" );
else
Console.WriteLine( "1" );
return ;
}
min_element_removal(arr, N);
}
public static void Main(String[] args)
{
int []arr = { 2, 1, 1, 5, 6, 2, 3, 1 };
int N = arr.Length;
makeBitonic(arr, N);
}
}
|
Javascript
<script>
function min_element_removal(arr, N)
{
var left = Array(N).fill(1);
var right = Array(N).fill(1);
for ( var i = 1; i < N; i++) {
for ( var j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
left[i] = Math.max(left[i],
left[j] + 1);
}
}
}
for ( var i = N - 2; i >= 0;
i--) {
for ( var j = N - 1; j > i;
j--) {
if (arr[i] > arr[j]) {
right[i] = Math.max(right[i],
right[j] + 1);
}
}
}
var maxLen = 0;
for ( var i = 1; i < N - 1; i++) {
maxLen = Math.max(maxLen, left[i] + right[i] - 1);
}
document.write((N - maxLen) + "<br>" );
}
function makeBitonic(arr, N)
{
if (N == 1) {
document.write( "0" + "<br>" );
return ;
}
if (N == 2) {
if (arr[0] != arr[1])
document.write( "0" + "<br>" );
else
document.write( "1" + "<br>" );
return ;
}
min_element_removal(arr, N);
}
var arr = [2, 1, 1, 5, 6, 2, 3, 1];
var N = arr.length;
makeBitonic(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Optimized Approach:
This approach has a time complexity of O(n), where n is the number of elements in the array, since it takes a single pass through the array to find the local maximum and local minimum, and it performs a constant amount of work for each iteration of the loop.
C++
#include <bits/stdc++.h>
using namespace std;
void min_element_removal( int arr[], int N)
{
int l = 0, r = N-1, count = 0;
while (l < r)
{
while (l < N-1 && arr[l] <= arr[l+1])
l++;
while (r > 0 && arr[r] <= arr[r-1])
r--;
if (l < r)
{
l++;
r--;
count++;
}
}
cout << count << endl;
}
int main()
{
int arr[] = { 5,7,1,3,6,2,1 };
int N = sizeof (arr) / sizeof (arr[0]);
min_element_removal(arr, N);
return 0;
}
|
Java
import java.util.*;
class Main {
public static void min_element_removal( int arr[], int N)
{
int l = 0 , r = N - 1 , count = 0 ;
while (l < r) {
while (l < N - 1 && arr[l] <= arr[l + 1 ])
l++;
while (r > 0 && arr[r] <= arr[r - 1 ])
r--;
if (l < r) {
l++;
r--;
count++;
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int arr[] = { 5 , 7 , 1 , 3 , 6 , 2 , 1 };
int N = arr.length;
min_element_removal(arr, N);
}
}
|
Python3
def min_element_removal(arr, N):
l, r, count = 0 , N - 1 , 0
while l < r:
while l < N - 1 and arr[l] < = arr[l + 1 ]:
l + = 1
while r > 0 and arr[r] < = arr[r - 1 ]:
r - = 1
if l < r:
l + = 1
r - = 1
count + = 1
print (count)
arr = [ 5 , 7 , 1 , 3 , 6 , 2 , 1 ]
N = len (arr)
min_element_removal(arr, N)
|
Javascript
function min_element_removal(arr, N) {
let l = 0, r = N-1, count = 0;
while (l < r) {
while (l < N-1 && arr[l] <= arr[l+1]) {
l++;
}
while (r > 0 && arr[r] <= arr[r-1]) {
r--;
}
if (l < r) {
l++;
r--;
count++;
}
}
console.log(count);
}
let arr = [5, 7, 1, 3, 6, 2, 1];
let N = arr.length;
min_element_removal(arr, N);
|
C#
using System;
class Program
{
static void Main( string [] args)
{
int [] arr = { 5, 7, 1, 3, 6, 2, 1 };
int N = arr.Length;
int l = 0, r = N - 1, count = 0;
while (l < r)
{
while (l < N - 1 && arr[l] <= arr[l + 1])
l++;
while (r > 0 && arr[r] <= arr[r - 1])
r--;
if (l < r)
{
l++;
r--;
count++;
}
}
Console.WriteLine(count);
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
02 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...