The Indian Engineer

Problem 2554 Maximum Number of Integers to Choose From a Range I

Posted on 5 mins

Array Hash-Table Binary-Search Greedy Sorting

Problem Statement

Link - Problem 2554

Question

You are given an integer array banned and two integers n and maxSum. You are choosing some number of integers following the below rules:

Return the maximum number of integers you can choose following the mentioned rules.

Example 1:

Input: banned = [1,6,5], n = 5, maxSum = 6
Output: 2
Explanation: You can choose the integers 2 and 4.
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.

Example 2:

Input: banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
Output: 0
Explanation: You cannot choose any integer while following the mentioned conditions.

Example 3:

Input: banned = [11], n = 7, maxSum = 50
Output: 7
Explanation: You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.

Constraints:

Solution

class Solution {
public:
    int maxCount(vector<int>& banned, int n, int maxSum) {
        vector<bool>valid(1e4+1,true);

        for(auto it:banned)
            valid[it] = false;

        n=n+1;
        int count = 0;

        for(int i = 1; i<n;i++){
            if(!valid[i])
                continue;

            if(maxSum>=i){
                maxSum-=i;
                count++;
            }
        }

        return count;
    }
};

Complexity Analysis

| Algorithm    | Time Complexity | Space Complexity |
| ------------ | --------------- | ---------------- |
| Linear Sweep | O(b+n)          | O(n+b)           |

Explanation

Intial Thoughts

First, understand the rules for choosing integers. Visualize a grid of numbers from 1 to n and cross out the banned ones. The goal is to pick as many numbers as possible without exceeding maxSum. Separate the banned numbers from the valid ones. Create a list or array to keep track of valid numbers.

Intuitive Analysis

Start from the smallest valid number and keep adding it to the running total until it reaches maxSum. Think of this process as pouring water into a jar - we fill the jar with the smallest amounts first. If a number exceeds maxSum, we stop and return the current count of valid numbers. Imagine the count as the number of ‘stones’ we’ve added to our jar, where each stone represents a valid number. This approach ensures we make the most of the sum limit while adhering to the rules.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Keep the banned numbers that are less than n in a set.

Loop over the numbers from 1 to n and if the number is not banned, use it.

Keep adding numbers while they are not banned, and their sum is less than k.


Similar Questions:

Title URL Difficulty
First Missing Positive https://leetcode.com/problems/first-missing-positive Hard
Find All Numbers Disappeared in an Array https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array Easy
Append K Integers With Minimal Sum https://leetcode.com/problems/append-k-integers-with-minimal-sum Medium
Replace Elements in an Array https://leetcode.com/problems/replace-elements-in-an-array Medium
Maximum Number of Integers to Choose From a Range II https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-ii Medium