Maximum removal from array when removal time >= waiting time
Last Updated :
15 Mar, 2023
Given there are N elements in an array. The task is to remove elements from the array from left to right. However, some time is required to remove an element from the array(let us call it removal time). The time to remove an element is equal to the value of that element in seconds.
An element can only be removed when the time required to remove it(removal time) is greater than or equal to the time it waits in the array.
Note: It is allowed to change the order of elements in the array before starting to remove elements. Your task is to find the maximum number of elements which can be removed from the array.
Examples:
Input : arr[] = {6, 5, 11, 3}
Output : 3
Explanation : Let us reorder the elements in the following way:
3, 5, 6, 11
-The first element takes 3 seconds to get removed. Since it is the first element, it can be removed in 3 seconds.
-The second element waits 3 seconds in the array. This element takes 5 seconds to get removed, which is more than it’s waiting time, hence it can be removed.
-The third element waits 8 seconds in the array. This element takes 6 seconds to get removed, which is less than it’s waiting time, hence it cannot be removed and it is skipped.
-The fourth element also waits 8 seconds in the array. This element takes 11 seconds to get removed, which is more than it’s waiting time, hence it can be removed.
-Hence, a maximum of 3 elements can be removed.
Input : arr[] = {5, 4, 1, 10}
Output : 4
Explanation: Let us reorder the elements in the following way:
1, 4, 5, 10
It can be observed that all of them can be removed since each element’s removal time is greater or equal to their waiting time.
The idea is to arrange all the elements in ascending order of their removal time. Start iterating from left side and maintain a cumulative sum of the removal time (which will serve as the waiting time for next element). Check at each element, if it’s removal time is greater than or equal to the cumulative time(it’s waiting time). If it is less, than it cannot be removed. If it is equal or greater, than it can be removed and add it’s removal time in cumulative sum. Proceed till the end of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxRemoval( int arr[], int n)
{
int count = 0;
int cumulative_sum = 0;
sort(arr, arr + n);
for ( int i = 0; i < n; i++) {
if (arr[i] >= cumulative_sum) {
count++;
cumulative_sum += arr[i];
}
}
return count;
}
int main()
{
int arr[] = { 10, 5, 3, 7, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxRemoval(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int maxRemoval( int arr[], int n)
{
int count = 0 ;
int cumulative_sum = 0 ;
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++) {
if (arr[i] >= cumulative_sum) {
count++;
cumulative_sum += arr[i];
}
}
return count;
}
public static void main (String[] args) {
int arr[] = { 10 , 5 , 3 , 7 , 2 };
int n = arr.length;
System.out.println(maxRemoval(arr, n));
}
}
|
Python3
def maxRemoval(arr, n):
count = 0
cumulative_sum = 0
arr.sort()
for i in range (n):
if arr[i] > = cumulative_sum:
count + = 1
cumulative_sum + = arr[i]
return count
if __name__ = = "__main__" :
arr = [ 10 , 5 , 3 , 7 , 2 ]
n = len (arr)
print (maxRemoval(arr, n))
|
C#
using System;
class GFG
{
static int maxRemoval( int [] arr, int n)
{
int count = 0;
int cumulative_sum = 0;
Array.Sort(arr);
for ( int i = 0; i < n; i++)
{
if (arr[i] >= cumulative_sum)
{
count++;
cumulative_sum += arr[i];
}
}
return count;
}
public static void Main ()
{
int [] arr = { 10, 5, 3, 7, 2 };
int n = arr.Length;
Console.Write(maxRemoval(arr, n));
}
}
|
PHP
<?php
function maxRemoval(& $arr , $n )
{
$count = 0;
$cumulative_sum = 0;
sort( $arr );
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] >= $cumulative_sum )
{
$count ++;
$cumulative_sum += $arr [ $i ];
}
}
return $count ;
}
$arr = array (10, 5, 3, 7, 2 );
$n = sizeof( $arr );
echo (maxRemoval( $arr , $n ));
?>
|
Javascript
<script>
function maxRemoval(arr, n)
{
var count = 0;
var cumulative_sum = 0;
arr.sort((a,b)=> a-b);
for ( var i = 0; i < n; i++) {
if (arr[i] >= cumulative_sum) {
count++;
cumulative_sum += arr[i];
}
}
return count;
}
var arr = [ 10, 5, 3, 7, 2 ];
var n = arr.length;
document.write( maxRemoval(arr, n));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...