Open In App

Steps to make array empty by removing maximum and its right side

Last Updated : 19 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to simulate max deletion
// and calculate number of steps.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find index of the maximum number
// in the array of size n
int findMax(int arr[], int n)
{
    int max = 0, index = 0;
    for (int i = 0; i < n; i++)
  
        // Condition to get the maximum
        if (arr[i] > max) {
            max = arr[i];
            index = i;
        }
  
    // return the index of the maximum element
    return index;
}
  
int countSteps(int arr[], int n)
{
    // Find the index of largest number in the array
    int index = findMax(arr, n);
  
    //'steps' variable calculates the number of
    // steps being taken.
    int steps = 1;
  
    // Check until the first element of array is removed,
    // hence until index!=0
    while (index != 0) {
  
        // Update index with the index value of highest
        // element in the remaining array.
        index = findMax(arr, index);
        steps++;
    }
  
    return steps;
}
  
// Driver Code
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





Output

Steps Taken: 4

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]:
  
    # keep a count of number of times this
    # condition executes.
    max = arr[i]


C#




if (max < arr[i]) {
  
    // keep a count of number of times this
    // condition executes.
    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#




// C# code to simulate max
// deletion and calculate 
// number of steps.
using System;
  
class GFG {
  
    // Function to find the maximum
    // number in the array upto index
    // n and return its index.
    static int countSteps(int[] arr, int n)
    {
        int max = -1, steps = 0;
        for (int i = 0; i < n; i++)
        {
  
            // condition to find max
            if (arr[i] > max) {
  
                max = arr[i];
  
                // Count the number of times
                // this condition executes 
                // that will the number of 
                // turns being played.
                steps++;
            }
        }
  
        // return the number of turns played
        return steps;
    }
  
    /* Driver program to test above function */
    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));
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP program to simulate max deletion
// and calculate number of steps.
  
// Function to find the
// maximum number in the
// array upto index n and
// return its index.
function countSteps($arr, $n)
{
    $max = -1; 
    $steps = 0;
    for ( $i = 0; $i < $n; $i++)
    {
  
        // condition to find max
        if ($arr[$i] > $max
        {
  
            $max = $arr[$i];
  
            // Count the number 
            // of times this
            // condition executes
            // that will the number 
            // of turns being played.
            $steps++;
        }
    }
  
    // return the number 
    // of turns played
    return $steps;
}
  
    // Driver Code
    $arr = array(2, 5, 8, 24, 4, 
                11, 6, 1, 15, 10);
    $n = count($arr);
  
    //'steps' variable calculates
    // the number of steps being 
    // taken.
    echo "Steps Taken: ";
    echo countSteps($arr, $n);
      
// This code is contributed by anuj_67.
?>


Javascript




<script>
    // Javascript program to simulate max deletion
    // and calculate number of steps.
      
    // Function to find the maximum number in the
    // array upto index n and return its index.
    function countSteps(arr, n)
    {
        let max = -1, steps = 0;
        for (let i = 0; i < n; i++) {
  
            // condition to find max
            if (arr[i] > max) {
  
                max = arr[i];
  
                // Count the number of times this
                // condition executes that will the
                // number of turns being played.
                steps++;
            }
        }
  
        // return the number of turns played
        return steps;
    }
      
    let arr = [ 2, 5, 8, 24, 4, 11, 6, 1, 15, 10 ];
    let n = arr.length;
    
    //'steps' variable calculates the number of
    // steps being taken.
    document.write("Steps Taken: ");
    document.write(countSteps(arr, n));
  
</script>


Output

Steps Taken: 4

Time complexity of this approach is O(n).
Auxiliary Space: O(1) 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads