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

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to simulate max deletion
// and calculate number of steps.
import java.util.*;
  
class GFG {
  
    // Function to find index of the maximum
    // number in the array of size n
    static 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;
    }
  
    static 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 program to test above function */
    public static void main(String[] args)
    {
        int arr[] = { 2, 5, 8, 24, 4, 11,
                      6, 1, 15, 10 };
        int n = arr.length;
  
        System.out.print("Steps Taken: ");
        System.out.println(countSteps(arr, n));
    }
}
// This code is contributed by Arnav Kr. Mandal.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to simulate max deletion
# and calculate number of steps.
  
# Function to find index of the maximum number
# in the array of size n
def findMax(arr, n):
    large, index = 0, 0
  
    for i in range(n):
  
        # Condition to get the maximum
        if arr[i] > large:
            large = arr[i]
            index = i
  
    # return the index of the maximum element
    return index
  
  
def countSteps(arr, n):
  
    # Find the index of largest number in the array
    index = findMax(arr, n)
  
    #'steps' variable calculates the number of
    # steps being taken.
    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 += 1
  
    return steps
  
  
# Driver Code
if __name__ == "__main__":
    arr = [2, 5, 8, 24, 4, 11, 6, 1, 15, 10]
    n = len(arr)
    print("Steps Taken:", countSteps(arr, n))
  
# This code is contributed by
# sanjeev2552

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to simulate max
// deletion and calculate
// number of steps.
using System;
  
class GFG {
  
    // Function to find index
    // of the maximum number
    // in the array of size n
    static 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;
    }
  
    static 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 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to simulate max deletion
// and calculate number of steps.
  
// Function to find index of the maximum 
// number in the array of size n
function findMax($arr, $n)
{
    $max = 0; $index = 0;
    for ( $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;
}
  
function countSteps($arr, $n)
{
    // Find the index of largest
    // number in the array
    $index = findMax($arr, $n);
  
    //'steps' variable calculates 
    // the number of steps being taken.
    $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
    $arr = array(2, 5, 8, 24, 4, 
              11, 6, 1, 15, 10);
    $n = sizeof($arr);
    echo "Steps Taken: ";
    echo countSteps($arr, $n) ,"\n";
  
// This code is contributed by ajit. 
?>

chevron_right



Output:

Steps Taken: 4

The time complexity of this approach is O(n^2).

 

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

if (max < arr[i]) {
  
    // keep a count of number of times this
    // condition executes.
    max = arr[i];
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to simulate max deletion
// and calculate number of steps.
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the maximum number in the
// array upto index n and return its index.
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 Code
int main()
{
    int arr[] = { 2, 5, 8, 24, 4, 11, 6, 1, 15, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    //'steps' variable calculates the number of
    // steps being taken.
    cout << "Steps Taken: ";
    cout << countSteps(arr, n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to simulate max deletion
// and calculate number of steps.
import java.util.*;
  
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(String[] args)
    {
        int arr[] = { 2, 5, 8, 24, 4, 11, 6,
                      1, 15, 10 };
        int n = arr.length;
  
        System.out.print("Steps Taken: ");
        System.out.println(countSteps(arr, n));
    }
}
// This code is contributed by Arnav Kr. Mandal.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 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.
def countSteps(arr, n):
    large, steps = -1, 0
  
    for i in range(n):
  
        # condition to find max
        if arr[i] > large:
            large = arr[i]
  
            # Count the number of times this
            # condition executes that will the
            # number of turns being played.
            steps += 1
  
    # return the number of turns played
    return steps
  
  
# Driver Code
if __name__ == "__main__":
    arr = [2, 5, 8, 24, 4, 11, 6, 1, 15, 10]
    n = len(arr)
  
    #'steps' variable calculates the number of
    # steps being taken.
    print("Steps Taken:", countSteps(arr, n))
  
# This code is contributed by
# sanjeev2552

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right



Output:

Steps Taken: 4

The time complexity of this approach is O(n).

This article is contributed by DANISH KALEEM. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m, jit_t, sanjeev2552



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.