Maximum Number of Integers to Choose From a Range I
MEDIUMDescription
You are given an integer array banned and two integers n and maxSum. You are choosing some number of integers following the below rules:
- The chosen integers have to be in the range
[1, n]. - Each integer can be chosen at most once.
- The chosen integers should not be in the array
banned. - The sum of the chosen integers should not exceed
maxSum.
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:
1 <= banned.length <= 1041 <= banned[i], n <= 1041 <= maxSum <= 109
Approaches
Checkout 2 different approaches to solve Maximum Number of Integers to Choose From a Range I. Click on different approaches to view the approach and algorithm in detail.
Brute Force with Linear Scan
This approach uses a greedy strategy by iterating through numbers from 1 to n and picking the smallest available integers first. To check if a number is banned, it performs a simple linear scan through the banned array. This is the most straightforward but also the least efficient method.
Algorithm
- Initialize
countof chosen integers to 0 andcurrentSum(as a long to prevent overflow) to 0. - Iterate with a number
ifrom 1 ton. - For each
i, perform a linear scan through thebannedarray to check ifiis present. - If
iis not inbannedandcurrentSum + idoes not exceedmaxSum:- Increment
count. - Add
itocurrentSum.
- Increment
- If
currentSum + iexceedsmaxSum, stop the process as any subsequent number will also exceed the limit. - Return the final
count.
The core idea is to maximize the number of chosen integers. A greedy strategy works best here: by always choosing the smallest possible integers, we leave the maximum possible budget in maxSum for subsequent integers.
The algorithm proceeds as follows:
- Initialize a counter
countto 0 and a running sumcurrentSumto 0. We use alongforcurrentSumto avoid potential overflow sincemaxSumcan be large. - We loop through each integer
ifrom 1 ton. - For each
i, we first check if we can afford to add it. IfcurrentSum + i > maxSum, we break the loop because any number greater thaniwill also violate the condition. - If we can afford
i, we then check if it's a banned number. This is done by iterating through the entirebannedarray. A flag,isBanned, can be used to track this. - If
iis not banned, we choose it: incrementcountand additocurrentSum. - After the loop finishes (either by reaching
nor by exceedingmaxSum), we return the finalcount.
class Solution {
public int maxCount(int[] banned, int n, int maxSum) {
long currentSum = 0;
int count = 0;
for (int i = 1; i <= n; i++) {
if (currentSum + i > maxSum) {
break;
}
boolean isBanned = false;
for (int b : banned) {
if (b == i) {
isBanned = true;
break;
}
}
if (!isBanned) {
currentSum += i;
count++;
}
}
return count;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Very low memory usage as it doesn't require any extra data structures.
- Highly inefficient for the given constraints, with a time complexity of
O(n * B). This is likely to cause a 'Time Limit Exceeded' error on most coding platforms.
Code Solutions
Checking out 3 solutions in different languages for Maximum Number of Integers to Choose From a Range I. Click on different languages to view the code.
class Solution {
public
int maxCount(int[] banned, int n, int maxSum) {
Set<Integer> ban = new HashSet<>(banned.length);
for (int x : banned) {
ban.add(x);
}
int ans = 0, s = 0;
for (int i = 1; i <= n && s + i <= maxSum; ++i) {
if (!ban.contains(i)) {
++ans;
s += i;
}
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Maximum Number of Integers to Choose From a Range I
Similar Questions
5 related questions you might find useful
Algorithms:
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.