Steps to make array empty by removing maximum and its right side
Last Updated :
19 Sep, 2023
We are given an array of Integers. We have to perform the following operation on the array until it is fully exhausted.
- Select the max number in the array and delete that number including all the numbers to its right side in the array.
- Repeat the step 1 for the left elements of the array i.e select the maximum element in the left elements and delete it including all numbers to its right.
Our task is to simulate the above procedure and return the number of steps that will be taken until the first element(index 0) of the array is also deleted and array is said to be exhausted.
Examples:
Input : Array = [2, 3, 5, 4, 1]
Output : Steps Taken: 3
Explanation: Step 1: Remove 5 and elements to its right
so, Array becomes [2, 3]
Step 2: Remove 3 as it is the maximum and
right most already so, Array becomes [2]
Step 3: Remove 2 and the array becomes EMPTY
Hence, at the end of step 3 the array stands
exhausted.
Input : Array = [2, 5, 8, 24, 4, 11, 6, 1, 15, 10]
Output : Steps Taken: 4
Explanation: Step 1: Remove 24 and elements to its right
so, Array becomes [2, 5, 8]
Step 2: Remove 8 and elements to its right
so, Array becomes [2, 5]
Step 3: Remove 5 and elements to its right
so, Array becomes [2]
Step 4: Remove 2 and the array becomes EMPTY
Hence, at the end of step 4 the array stands
exhausted.
Naive Approach: A simple approach to the problem is to find the maximum value in the array and store its index and then again find the maximum value in the array between range 0 and the previously stored index. Repeat this procedure until the 0th indexed element is removed.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findMax( int arr[], int n)
{
int max = 0, index = 0;
for ( int i = 0; i < n; i++)
if (arr[i] > max) {
max = arr[i];
index = i;
}
return index;
}
int countSteps( int arr[], int n)
{
int index = findMax(arr, n);
int steps = 1;
while (index != 0) {
index = findMax(arr, index);
steps++;
}
return steps;
}
int main()
{
int arr[] = { 2, 5, 8, 24, 4, 11, 6, 1, 15, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Steps Taken: " ;
cout << countSteps(arr, n) << endl;
return 0;
}
|
Java
Python 3
C#
PHP
Javascript
Time complexity of this approach is O(n^2).
Auxiliary Space: O(1)
Efficient Approach:
An efficient approach is to initialize max as -1 and while calculating the max in the array count the number of times the swapping conditions executes i.e This condition:
CPP
Java
Javascript
Python3
if max < arr[i]:
max = arr[i]
|
C#
if (max < arr[i]) {
max = arr[i];
}
|
Every-time swapping occurs it is guaranteed that the previous numbers in the array were smaller than the current element.This gives us exact numbers of steps that will take place for the given set of integers.
C++
Java
Python 3
C#
using System;
class GFG {
static int countSteps( int [] arr, int n)
{
int max = -1, steps = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] > max) {
max = arr[i];
steps++;
}
}
return steps;
}
public static void Main()
{
int [] arr = { 2, 5, 8, 24, 4, 11, 6,
1, 15, 10 };
int n = arr.Length;
Console.Write( "Steps Taken: " );
Console.WriteLine(countSteps(arr, n));
}
}
|
PHP
<?php
function countSteps( $arr , $n )
{
$max = -1;
$steps = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] > $max )
{
$max = $arr [ $i ];
$steps ++;
}
}
return $steps ;
}
$arr = array (2, 5, 8, 24, 4,
11, 6, 1, 15, 10);
$n = count ( $arr );
echo "Steps Taken: " ;
echo countSteps( $arr , $n );
?>
|
Javascript
<script>
function countSteps(arr, n)
{
let max = -1, steps = 0;
for (let i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
steps++;
}
}
return steps;
}
let arr = [ 2, 5, 8, 24, 4, 11, 6, 1, 15, 10 ];
let n = arr.length;
document.write( "Steps Taken: " );
document.write(countSteps(arr, n));
</script>
|
Time complexity of this approach is O(n).
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...