Minimum steps required to make an array decreasing
Last Updated :
21 Aug, 2023
Given an array arr[], the task is to find the minimum steps required to make an array decreasing, where in each step remove all elements which are greater than elements on its left element.
Examples:
Input: arr[] = {3, 2, 1, 7, 5}
Output: 2
Explanation:
In the above array there are two steps required to make array decreasing –
Step 1: In step 1 there is one element which is greater than its left, that is 7 > 1.
Step 2: In step 2 there is one element which is greater than its left, that is 5 > 1.
Input: arr[] = {6, 5, 8, 4, 7, 10, 9}
Output: 2
In the above array there are two steps required to make array decreasing –
Step 1: In step 1 there are three elements which is greater than its left, which is 8 > 5, 7 > 4 and 10 > 7.
Step 2: In step 2 there are three is only one element which is greater than its left which is 9 > 4.
Naive Approach: Iterate over the array and count the elements which are greater than its left, if the element is not greater than its left then push the element into another array/vector (say arr1) and after the complete iteration of the array copy all the elements of arr1 to the initial array and repeat the same procedure until the count of elements removed in a step is 0. This approach takes O(N2) time in the worst case and O(N) space.
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void minSteps( int arr[], int N)
{
vector< int > arr1;
int size = N;
int count;
int ans = 0;
while ( true ) {
count = 0;
arr1.push_back(arr[0]);
for ( int i = 1; i < size; i++) {
if (arr[i] <= arr[i - 1]) {
arr1.push_back(arr[i]);
}
else {
count++;
}
}
if (count == 0) {
break ;
}
size = arr1.size();
for ( int i = 0; i < size; i++) {
arr[i] = arr1[i];
}
arr1.clear();
ans++;
}
cout << ans << endl;
}
int main()
{
int arr[] = { 3, 2, 1, 7, 5 };
int size = sizeof (arr) / sizeof (arr[0]);
minSteps(arr, size);
return 0;
}
|
Java
import java.util.ArrayList;
public class GFG {
public static void minSteps( int [] arr) {
ArrayList<Integer> arr1 = new ArrayList<>();
int size = arr.length;
int count;
int ans = 0 ;
while ( true ) {
count = 0 ;
arr1.add(arr[ 0 ]);
for ( int i = 1 ; i < size; i++) {
if (arr[i] <= arr[i - 1 ]) {
arr1.add(arr[i]);
}
else {
count++;
}
}
if (count == 0 ) {
break ;
}
size = arr1.size();
for ( int i = 0 ; i < size; i++) {
arr[i] = arr1.get(i);
}
arr1.clear();
ans++;
}
System.out.println(ans);
}
public static void main(String[] args) {
int [] arr = { 3 , 2 , 1 , 7 , 5 };
minSteps(arr);
}
}
|
Python3
import sys
def minSteps(arr, N):
arr1 = []
size = N
count = 0
ans = 0
while True :
count = 0
arr1.append(arr[ 0 ])
for i in range ( 1 , size):
if arr[i] < = arr[i - 1 ]:
arr1.append(arr[i])
else :
count + = 1
if count = = 0 :
break
size = len (arr1)
for i in range (size):
arr[i] = arr1[i]
arr1.clear()
ans + = 1
print (ans)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 , 7 , 5 ]
size = len (arr)
minSteps(arr, size)
|
C#
using System;
public class GFG
{
public static void MinSteps( int [] arr, int N)
{
var arr1 = new System.Collections.Generic.List< int >();
int size = N;
int count;
int ans = 0;
while ( true )
{
count = 0;
arr1.Add(arr[0]);
for ( int i = 1; i < size; i++)
{
if (arr[i] <= arr[i - 1])
{
arr1.Add(arr[i]);
}
else
{
count++;
}
}
if (count == 0)
{
break ;
}
size = arr1.Count;
for ( int i = 0; i < size; i++)
{
arr[i] = arr1[i];
}
arr1.Clear();
ans++;
}
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int [] arr = { 3, 2, 1, 7, 5 };
int size = arr.Length;
MinSteps(arr, size);
}
}
|
Javascript
function minSteps(arr, N)
{
let arr1 = [];
let size = N;
let count;
let ans = 0;
while ( true ) {
count = 0;
arr1.push(arr[0]);
for (let i = 1; i < size; i++) {
if (arr[i] <= arr[i - 1]) {
arr1.push(arr[i]);
}
else {
count++;
}
}
if (count == 0) {
break ;
}
size = arr1.length;
for (let i = 0; i < size; i++) {
arr[i] = arr1[i];
}
arr1 = [];
ans++;
}
console.log(ans);
}
let arr = [ 3, 2, 1, 7, 5 ];
let size = arr.length;
minSteps(arr, size);
|
Time Complexity-O(N2)
Auxiliary Space-O(N)
Efficient Approach: The idea is to use a stack and push the element into the stack only if the element is greater than its previous element, else count the number of scans and pop.
We only care about the elements which is smaller.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int elementID;
int stepsToeliminate;
};
void minSteps( int arr[], int N)
{
stack<Node> s;
s.push({ 0, -1 });
int maxStepsToeliminate = -1;
for ( int i = 1; i < N; i++) {
int stepsToeliminate = 1;
while (!s.empty()) {
if (arr[s.top().elementID] >= arr[i]) {
stepsToeliminate
= max(stepsToeliminate,
s.top().stepsToeliminate + 1);
s.pop();
}
else {
break ;
}
}
if (s.empty()) {
stepsToeliminate = -1;
}
maxStepsToeliminate
= max(maxStepsToeliminate, stepsToeliminate);
s.push({ i, stepsToeliminate });
}
cout << (maxStepsToeliminate < 0 ? 0
: maxStepsToeliminate)
<< endl;
}
int main()
{
int arr[] = { 3, 2, 1, 7, 5 };
int size = sizeof (arr) / sizeof (arr[0]);
minSteps(arr, size);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
int elementID;
int stepsToeliminate;
public Node( int elementID, int stepsToeliminate)
{
super ();
this .elementID = elementID;
this .stepsToeliminate = stepsToeliminate;
}
};
static void minSteps( int arr[], int N)
{
Stack<Node> s = new Stack<Node>();
s.add( new Node( 0 , - 1 ));
int maxStepsToeliminate = - 1 ;
for ( int i = 1 ; i < N; i++) {
int stepsToeliminate = 1 ;
while (!s.isEmpty()) {
if (arr[s.peek().elementID] >= arr[i]) {
stepsToeliminate = Math.max(
stepsToeliminate,
s.peek().stepsToeliminate + 1 );
s.pop();
}
else {
break ;
}
}
if (s.isEmpty()) {
stepsToeliminate = - 1 ;
}
maxStepsToeliminate = Math.max(
maxStepsToeliminate, stepsToeliminate);
s.add( new Node(i, stepsToeliminate));
}
System.out.print((maxStepsToeliminate < 0
? 0
: maxStepsToeliminate)
+ "\n" );
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 1 , 7 , 5 };
int size = arr.length;
minSteps(arr, size);
}
}
|
Python3
def minSteps(arr, N):
s = []
s.append(( 0 , - 1 ))
maxStepsToeliminate = - 1
for i in range ( 1 , N):
stepsToeliminate = 1
while ( len (s) ! = 0 ):
if (arr[s[ - 1 ][ 0 ]] > = arr[i]):
stepsToeliminate = max (stepsToeliminate, s[ - 1 ][ 1 ] + 1 )
s.pop()
else :
break
if ( len (s) = = 0 ):
stepsToeliminate = - 1
maxStepsToeliminate = max (maxStepsToeliminate, stepsToeliminate)
s.append((i, stepsToeliminate))
print ( 0 if (maxStepsToeliminate < 0 ) else maxStepsToeliminate)
if __name__ = = "__main__" :
arr = [ 3 , 2 , 1 , 7 , 5 ]
size = len (arr)
minSteps(arr, size)
|
C#
using System;
using System.Collections.Generic;
class GFG {
class Node {
public int elementID;
public int stepsToeliminate;
public Node( int elementID, int stepsToeliminate)
{
this .elementID = elementID;
this .stepsToeliminate = stepsToeliminate;
}
};
static void minSteps( int [] arr, int N)
{
Stack<Node> s = new Stack<Node>();
s.Push( new Node(0, -1));
int maxStepsToeliminate = -1;
for ( int i = 1; i < N; i++) {
int stepsToeliminate = 1;
while (s.Count != 0) {
if (arr[s.Peek().elementID] >= arr[i]) {
stepsToeliminate = Math.Max(
stepsToeliminate,
s.Peek().stepsToeliminate + 1);
s.Pop();
}
else {
break ;
}
}
if (s.Count != 0) {
stepsToeliminate = -1;
}
maxStepsToeliminate = Math.Max(
maxStepsToeliminate, stepsToeliminate);
s.Push( new Node(i, stepsToeliminate));
}
Console.Write((maxStepsToeliminate < 0
? 0
: maxStepsToeliminate)
+ "\n" );
}
public static void Main(String[] args)
{
int [] arr = { 3, 2, 1, 7, 5 };
int size = arr.Length;
minSteps(arr, size);
}
}
|
Javascript
<script>
class Node
{
constructor(elementID, stepsToeliminate) {
this .elementID = elementID;
this .stepsToeliminate = stepsToeliminate;
}
}
function minSteps(arr, N)
{
let s = [];
s.push( new Node( 0, -1 ));
let maxStepsToeliminate = -1;
for (let i = 1; i < N; i++)
{
let stepsToeliminate = 1;
while (s.length!=0)
{
if (arr[s[s.length - 1].elementID] >= arr[i])
{
stepsToeliminate = Math.max(stepsToeliminate,
s[s.length - 1].stepsToeliminate + 1);
s.pop();
}
else
{
break ;
}
}
if (s.length!=0)
{
stepsToeliminate = -1;
}
maxStepsToeliminate = Math.max(maxStepsToeliminate, stepsToeliminate);
s.push( new Node(i, stepsToeliminate ));
}
document.write((maxStepsToeliminate < 0 ? 0 :
maxStepsToeliminate) + "</br>" );
}
let arr = [3, 2, 1, 7, 5];
let size = arr.length;
minSteps(arr, size);
</script>
|
Performance Analysis:
- Time Complexity: As in the above approach, there is one loop which takes O(N) time, Hence the Time Complexity will be O(N).
- Space Complexity: As in the above approach, there is stack used to store the previous elements, Hence the space complexity will be O(N).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...