Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Maximum removal from array when removal time >= waiting time
Next article icon

Maximum removal from array when removal time >= waiting time

Last Updated : 15 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// C++ code to find the maximum number of // elements that can be removed #include <bits/stdc++.h> using namespace std;  // Function to find maximum number of // elements that can be removed int maxRemoval(int arr[], int n) {     // it will contain frequency of     // elements that can be removed     int count = 0;      // maintain cumulative sum of removal time     int cumulative_sum = 0;      // arrange elements in ascending order     // of their removal time     sort(arr, arr + n);      for (int i = 0; i < n; i++) {         if (arr[i] >= cumulative_sum) {             count++;             cumulative_sum += arr[i];         }     }      return count; }  // Driver code int main() {     int arr[] = { 10, 5, 3, 7, 2 };     int n = sizeof(arr) / sizeof(arr[0]);      cout << maxRemoval(arr, n);      return 0; } 
Java
// Java code to find the maximum number of // elements that can be removed import java.io.*; import java.util.*;  class GFG {  // Function to find maximum number of // elements that can be removed static int maxRemoval(int arr[], int n) {     // it will contain frequency of     // elements that can be removed     int count = 0;      // maintain cumulative sum of removal time     int cumulative_sum = 0;      // arrange elements in ascending order     // of their removal time     Arrays.sort(arr);      for (int i = 0; i < n; i++) {         if (arr[i] >= cumulative_sum) {             count++;             cumulative_sum += arr[i];         }     }      return count; }  // Driver code      public static void main (String[] args) {         int arr[] = { 10, 5, 3, 7, 2 };     int n = arr.length;     System.out.println(maxRemoval(arr, n));     } } // This code is contributed  // by inder_verma.. 
Python3
# Python3 code to find the maximum number  # of elements that can be removed   # Function to find maximum number of  # elements that can be removed  def maxRemoval(arr, n):       # It will contain frequency of      # elements that can be removed      count = 0      # maintain cumulative sum of      # removal time      cumulative_sum = 0      # arrange elements in ascending      # order of their removal time      arr.sort()      for i in range(n):          if arr[i] >= cumulative_sum:              count += 1             cumulative_sum += arr[i]       return count   # Driver code  if __name__ == "__main__":      arr = [10, 5, 3, 7, 2]      n = len(arr)       print(maxRemoval(arr, n))   # This code is contributed by # Rituraj Jain 
C#
// C# code to find the maximum number  // of elements that can be removed using System;   class GFG {  // Function to find maximum number // of elements that can be removed static int maxRemoval(int[] arr, int n) {     // it will contain frequency of     // elements that can be removed     int count = 0;      // maintain cumulative sum      // of removal time     int cumulative_sum = 0;      // arrange elements in ascending      // order of their removal time     Array.Sort(arr);      for (int i = 0; i < n; i++)     {         if (arr[i] >= cumulative_sum)         {             count++;             cumulative_sum += arr[i];         }     }      return count; }  // Driver code public static void Main () {     int[] arr = { 10, 5, 3, 7, 2 };     int n = arr.Length;     Console.Write(maxRemoval(arr, n)); } }  // This code is contributed  // by ChitraNayal 
PHP
<?php // PHP code to find the maximum  // number of elements that can // be removed  // Function to find maximum number  // of elements that can be removed function maxRemoval(&$arr, $n) {     // it will contain frequency of     // elements that can be removed     $count = 0;      // maintain cumulative      // sum of removal time     $cumulative_sum = 0;      // arrange elements in ascending      // order of their removal time     sort($arr);      for ($i = 0; $i < $n; $i++)      {         if ($arr[$i] >= $cumulative_sum)         {             $count++;             $cumulative_sum += $arr[$i];         }     }      return $count; }  // Driver code $arr = array(10, 5, 3, 7, 2 ); $n = sizeof($arr);  echo (maxRemoval($arr, $n));  // This code is contributed  // by Shivi_Aggarwal  ?> 
JavaScript
<script>  // Javascript code to find the maximum number of // elements that can be removed  // Function to find maximum number of // elements that can be removed function maxRemoval(arr, n) {     // it will contain frequency of     // elements that can be removed     var count = 0;      // maintain cumulative sum of removal time     var cumulative_sum = 0;      // arrange elements in ascending order     // of their removal time     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; }  // Driver code var arr = [ 10, 5, 3, 7, 2 ]; var n = arr.length; document.write( maxRemoval(arr, n));  </script>  

Output: 
4

 

Time Complexity: O(nlogn)
Auxiliary Space: O(1)


Next Article
Maximum removal from array when removal time >= waiting time

S

Shashank_Sharma
Improve
Article Tags :
  • Misc
  • Algorithms
  • DSA
  • Arrays
  • Greedy Algorithms
  • Constructive Algorithms
Practice Tags :
  • Algorithms
  • Arrays
  • Misc

Similar Reads

    Minimum removals from array to make max - min <= K
    Given N integers and K, find the minimum number of elements that should be removed, such that Amax-Amin<=K. After the removal of elements, Amax and Amin is considered among the remaining elements. Examples: Input : a[] = {1, 3, 4, 9, 10, 11, 12, 17, 20}, k = 4 Output : 5 Explanation: Remove 1, 3,
    15+ min read
    Minimize Maximum Waiting Time for Students and Buses
    Given N number of students and M number of buses with each of them having a capacity of C. The time of arrival of N students is given by an array arr[] where i'th index (1 ≤ i ≤ N) denotes the time of arrival of the ith student, the task is to find the smallest value of the maximum waiting time any
    8 min read
    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
    14 min read
    Remove minimum elements from the array such that 2*min becomes more than max
    Given an unsorted array, arr[]. The task is to find the minimum number of removals required such that twice the minimum element in the array is greater than or equal to the maximum in the array.Examples: Input: arr[] = [4, 5, 100, 9, 10, 11, 12, 15, 200]Output: 4Explanation: In the given array 4 ele
    7 min read
    Maximize sum of selected numbers from Array to empty it | Set 2
    Given an array arr[] of N integers, the task is to maximize the sum of selected numbers over all the operations such that in each operation, choose a number Ai, delete one occurrence of it and delete all occurrences of Ai - 1 and Ai + 1 (if they exist) in the array until the array gets empty. Exampl
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences