Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
Given an array arr[] of non-negative integers. We have to delete elements from this array such that arr[i + 1] > arr[j] for each valid i and this will be counted as one step. We have to apply the same operations until the array has become strictly decreasing. Now the task is to count the number of steps required to get the desired array. Examples:
Input: arr[] = {6, 5, 8, 4, 7, 10, 9} Output: 2 Initially 8, 7 and 10 do not satisfy the condition so they all are deleted in the first step and the array becomes {6, 5, 4, 9} In the next step 9 gets deleted and the array becomes {6, 5, 4} which is strictly decreasing. Input: arr[] = {1, 2, 3, 4, 5} Output: 1
Approach: The idea is to keep the indices of only required elements that are to be checked against a particular element. Thus, we use a vector to store only the required indices. We insert every index at the back and then remove the indices from back if the following condition is satisfied.
arr[vect.back()] ? val[i]
We take another array in which we update the no of steps particular element takes to delete. If status[i] = -1 then element is not to be deleted, 0 denotes first step and so on…. That’s why we will add 1 to the answer. While popping the indices, we repeatedly update the status of elements. If all indices are popped i.e. vect.size() = 0 then this element is not to be deleted so change its status to -1. Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int status[100000];
int countSteps( int * val, int n)
{
int sol = 0;
vector< int > vec(1, 0);
status[0] = -1;
for ( int i = 1; i < n; ++i) {
status[i] = 0;
while (vec.size() > 0
&& val[vec.back()] >= val[i]) {
status[i] = max(status[i],
status[vec.back()] + 1);
vec.pop_back();
}
if (vec.size() == 0) {
status[i] = -1;
}
vec.push_back(i);
sol = max(sol, status[i] + 1);
}
return sol;
}
int main()
{
int val[] = { 6, 5, 8, 4, 7, 10, 9 };
int n = sizeof (val) / sizeof (val[0]);
cout << countSteps(val, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int []status = new int [ 100000 ];
static int countSteps( int []val, int n)
{
int sol = 0 ;
Vector<Integer> vec = new Vector<>( 1 );
vec.add( 0 );
status[ 0 ] = - 1 ;
for ( int i = 1 ; i < n; ++i)
{
status[i] = 0 ;
while (vec.size() > 0
&& val[vec.lastElement()] >= val[i])
{
status[i] = Math.max(status[i],
status[vec.lastElement()] + 1 );
vec.remove(vec.lastElement());
}
if (vec.isEmpty())
{
status[i] = - 1 ;
}
vec.add(i);
sol = Math.max(sol, status[i] + 1 );
}
return sol;
}
public static void main(String[] args)
{
int val[] = { 6 , 5 , 8 , 4 , 7 , 10 , 9 };
int n = val.length;
System.out.println(countSteps(val, n));
}
}
|
Python3
status = [ 0 ] * 100000 ;
def countSteps(val, n) :
sol = 0 ;
vec = [ 1 , 0 ];
status[ 0 ] = - 1 ;
for i in range (n) :
status[i] = 0 ;
while ( len (vec) > 0
and val[vec[ len (vec) - 1 ]] > = val[i]) :
status[i] = max (status[i],
status[ len (vec) - 1 ] + 1 );
vec.pop();
if ( len (vec) = = 0 ) :
status[i] = - 1 ;
vec.append(i);
sol = max (sol, status[i] + 1 );
return sol;
if __name__ = = "__main__" :
val = [ 6 , 5 , 8 , 4 , 7 , 10 , 9 ];
n = len (val);
print (countSteps(val, n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int []status = new int [100000];
static int countSteps( int []val, int n)
{
int sol = 0;
List< int > vec = new List< int >(1);
vec.Add(0);
status[0] = -1;
for ( int i = 1; i < n; ++i)
{
status[i] = 0;
while (vec.Count > 0
&& val[vec[vec.Count-1]] >= val[i])
{
status[i] = Math.Max(status[i],
status[vec[vec.Count-1]] + 1);
vec.Remove(vec[vec.Count-1]);
}
if (vec.Count == 0)
{
status[i] = -1;
}
vec.Add(i);
sol = Math.Max(sol, status[i] + 1);
}
return sol;
}
public static void Main(String[] args)
{
int []val = { 6, 5, 8, 4, 7, 10, 9 };
int n = val.Length;
Console.WriteLine(countSteps(val, n));
}
}
|
Javascript
let status = [];
function countSteps(val, n)
{
let sol = 0;
let vec = [0];
vec.push(0);
status[0] = -1;
for (let i = 1; i < n; ++i)
{
status[i] = 0;
while (vec.length > 0 && val[vec.length-1] >= val[i])
{
status[i] = Math.max(status[i],status[vec.length-1] + 1);
vec.pop();
}
if (vec.length==0)
{
status[i] = -1;
}
vec.push(i);
sol = Math.max(sol, status[i] + 1);
}
return sol + 1;
}
let val = [ 6, 5, 8, 4, 7, 10, 9 ];
let n = val.length;
console.log(countSteps(val, n));
|
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of elements in the array.
Auxiliary Space: O(100000), as we are using extra space for the array status
Last Updated :
21 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...