Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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




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

Python 3




# 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

C#




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

PHP




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

Javascript




<script>
    // Javascript 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)
    {
        let max = 0, index = 0;
        for (let 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
        let index = findMax(arr, n);
  
        //'steps' variable calculates
        // the number of steps being
        // taken.
        let 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;
    }
     
    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>

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




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

Java




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

Javascript




if (max < arr[i]) {
 
    // keep a count of number of times this
    // condition executes.
    max = arr[i];
}
 
// The code is contributed by Nidhi goel

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




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

Java




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

Python 3




# 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

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) 

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


My Personal Notes arrow_drop_up
Last Updated : 01 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials