Find the number of boxes to be removed - GeeksforGeeks

11 Aug, 2021
Given an array arr[] representing a sequence of piles of boxes where each and every box has the same height of 1 unit. Given that you are on the top of the first pile and need to reach the ground by moving from each pile starting from leftmost to rightmost.Constraints:  
One can move from the current pile of box to the next one when the height of the next pile is equal or less than the height of the pile on which they are standing.
One can also encounter some piles whose height is greater than the pile they are standing on. So, they will need to remove some boxes from that pile to move forward. So, the task is to tell the total number of boxes needed to be removed from every pile(if necessary) during the journey to the ground.
The height of all the piles is given. Suppose that you are standing on the first pile. Print the total number of boxes to be removed.Examples:  
Input : arr[] = {3, 3, 2, 4, 1} Output : 2 Explanation: After removing boxes, the heights of piles will be {3, 3, 2, 2, 1} We are currently standing on 1st pile of height 3. Step 1: We can move to the 2nd pile, since it’s height is equal to the height of the current pile. Step 2: We can move to the 3rd pile of height 2, since it less than 3. Step 3: We cannot go from 3rd pile to 4th pile(of height 4), so we need to remove 2 boxes from 4th pile to make it’s height equal to 2. Step 4: We can easily move to the last pile since it’s height is 1 which is less than the height of the 4th pile of height 2(by removing 2 boxes in the previous step).Input : arr[] = {5, 6, 7, 1} Output : 3 Explanation : After removing boxes, the heights of piles will be {5, 5, 5, 1} We are currently standing on 1st pile of height 5. Step 1: We cannot move to the 2nd pile since it’s height is more. So, we remove 1 box and make its height equal to 5 and then we move forward. Step 2: We cannot move to the 3rd pile of height 7, so we remove 2 boxes from it. Step 3: We can easily move to the last pile since it’s height is 1 which is less than the height of the 3rd pile of height 5.  
 
The idea is to traverse the array starting from left and every time before moving forward compare the height of the current pile with the previous pile. If the height of the current pile is greater than the previous pile, then increment count by the difference of the two heights otherwise move forward in the array.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the number of// boxes to be removed#include <bits/stdc++.h>using namespace std; // Function to find the number of// boxes to be removedint totalBoxesRemoved(int arr[], int n){    int count = 0;     // Store height of previous pile    int prev = arr[0];     // Start traversing the array    for (int i = 1; i < n; i++) {        // if height of current pile is greater        // than previous pile        if (arr[i] > prev) {            // Increment count by difference            // of two heights            count += (arr[i] - prev);             // Update current height            arr[i] = prev;             // Update prev for next iteration            prev = arr[i];        }        else {            // Update prev for next iteration            prev = arr[i];        }    }     return count;} // Driver codeint main(){    int arr[] = { 5, 4, 7, 3, 2, 1 };     int n = sizeof(arr) / sizeof(arr[0]);     cout << totalBoxesRemoved(arr, n);     return 0;}
// Java program to find the number of// boxes to be removedimport java.io.*; class GFG { // Function to find the number of// boxes to be removedstatic int totalBoxesRemoved(int arr[], int n){    int count = 0;     // Store height of previous pile    int prev = arr[0];     // Start traversing the array    for (int i = 1; i < n; i++) {        // if height of current pile is greater        // than previous pile        if (arr[i] > prev) {            // Increment count by difference            // of two heights            count += (arr[i] - prev);             // Update current height            arr[i] = prev;             // Update prev for next iteration            prev = arr[i];        }        else {            // Update prev for next iteration            prev = arr[i];        }    }     return count;}     // Driver code    public static void main (String[] args) {            int arr[] = { 5, 4, 7, 3, 2, 1 };     int n = arr.length;     System.out.println(totalBoxesRemoved(arr, n));    }} // This code is contributed// by inder_verma..
# Python3 program to find the# number of boxes to be removed # Function to find the number# of boxes to be removeddef totalBoxesRemoved(arr, n):     count = 0         # Store height of previous pile    prev = arr[0]     # Start traversing the array    for i in range(1, n):                 # if height of current pile        # is greater than previous pile        if (arr[i] > prev) :                         # Increment count by            # difference of two heights            count += (arr[i] - prev)             # Update current height            arr[i] = prev             # Update prev for next            # iteration            prev = arr[i]                 else :            # Update prev for next            # iteration            prev = arr[i]             return count # Driver codearr = [ 5, 4, 7, 3, 2, 1 ] n = len(arr) print(totalBoxesRemoved(arr, n)) # This code is contributed# by Yatin Gupta
// C# program to find the number of// boxes to be removedusing System; class GFG { // Function to find the number of// boxes to be removedstatic int totalBoxesRemoved(int []arr, int n){    int count = 0;     // Store height of previous pile    int prev = arr[0];     // Start traversing the array    for (int i = 1; i < n; i++) {        // if height of current pile is greater        // than previous pile        if (arr[i] > prev) {            // Increment count by difference            // of two heights            count += (arr[i] - prev);             // Update current height            arr[i] = prev;             // Update prev for next iteration            prev = arr[i];        }        else {            // Update prev for next iteration            prev = arr[i];        }    }     return count;}         // Driver code    public static void Main () {            int []arr = { 5, 4, 7, 3, 2, 1 };     int n = arr.Length;     Console.WriteLine(totalBoxesRemoved(arr, n));    }} // This code is contributed// by shs
<?php// PHP program to find the number// of boxes to be removed  // Function to find the number// of boxes to be removedfunction totalBoxesRemoved($arr, $n){    $count = 0;     // Store height of previous pile    $prev = $arr[0];     // Start traversing the array    for ($i = 1; $i <$n; $i++)    {        // if height of current pile is        // greater than previous pile        if ($arr[$i] > $prev)        {            // Increment count by difference            // of two heights            $count += ($arr[$i] - $prev);             // Update current height            $arr[$i] = $prev;             // Update prev for next iteration            $prev = $arr[$i];        }        else        {            // Update prev for next iteration            $prev = $arr[$i];        }    }     return $count;} // Driver code$arr = array( 5, 4, 7, 3, 2, 1 ); $n = count($arr); echo totalBoxesRemoved($arr, $n); // This code is contributed// by shs?>
<script> // Javascript program to find the number of// boxes to be removed // Function to find the number of// boxes to be removedfunction totalBoxesRemoved(arr, n){    var count = 0;     // Store height of previous pile    var prev = arr[0];     // Start traversing the array    for (var i = 1; i < n; i++) {        // if height of current pile is greater        // than previous pile        if (arr[i] > prev) {            // Increment count by difference            // of two heights            count += (arr[i] - prev);             // Update current height            arr[i] = prev;             // Update prev for next iteration            prev = arr[i];        }        else {            // Update prev for next iteration            prev = arr[i];        }    }     return count;} // Driver codevar arr = [5, 4, 7, 3, 2, 1 ];var n = arr.length;document.write( totalBoxesRemoved(arr, n)); </script>
3
 
Time Complexity: O(N), where N is the total number of piles.Auxiliary Space: O(1)  
inderDuMCA
YatinGupta
Shashank12
rrrtnx
pankajsharmagfg
Greedy Algorithms
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Understanding Time Complexity with Simple Examples
Introduction to Algorithms
How to write a Pseudo Code?
Arrays in Java
Arrays in C/C++
Program for array rotation
Stack Data Structure (Introduction and Program)
Largest Sum Contiguous Subarray