2554. Maximum Number of Integers to Choose From a Range I LeetCode Solution
In this guide, you will get 2554. Maximum Number of Integers to Choose From a Range I LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Integers to Choose From a Range I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Number of Integers to Choose From a Range I solution in C++
- Maximum Number of Integers to Choose From a Range I solution in Java
- Maximum Number of Integers to Choose From a Range I solution in Python
- Additional Resources

Problem Statement of Maximum Number of Integers to Choose From a Range I
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 <= 104
1 <= banned[i], n <= 104
1 <= maxSum <= 109
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2554. Maximum Number of Integers to Choose From a Range I LeetCode Solution in C++
class Solution {
public:
int maxCount(vector<int>& banned, int n, int maxSum) {
int ans = 0;
int sum = 0;
const unordered_set<int> bannedSet{banned.begin(), banned.end()};
for (int i = 1; i <= n; ++i)
if (!bannedSet.contains(i) && sum + i <= maxSum) {
++ans;
sum += i;
}
return ans;
}
};
/* code provided by PROGIEZ */
2554. Maximum Number of Integers to Choose From a Range I LeetCode Solution in Java
class Solution {
public int maxCount(int[] banned, int n, int maxSum) {
int ans = 0;
int sum = 0;
Set<Integer> bannedSet = Arrays.stream(banned).boxed().collect(Collectors.toSet());
for (int i = 1; i <= n; ++i)
if (!bannedSet.contains(i) && sum + i <= maxSum) {
++ans;
sum += i;
}
return ans;
}
}
// code provided by PROGIEZ
2554. Maximum Number of Integers to Choose From a Range I LeetCode Solution in Python
class Solution:
def maxCount(self, banned: list[int], n: int, maxSum: int) -> int:
ans = 0
summ = 0
bannedSet = set(banned)
for i in range(1, n + 1):
if i not in bannedSet and summ + i <= maxSum:
ans += 1
summ += i
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.