Maximum Number of Integers to Choose from a Range I
Try to solve the Maximum Number of Integers to Choose From a Range I problem.
We'll cover the following
Statement
Given an integer array banned
and two integers n
and maxSum
, determine the maximum number of integers you can choose while adhering to the following rules:
The selected integers must fall within the range
. Each integer can be chosen at most once.
No selected integer can be present in the
banned
array.The sum of the selected integers must not exceed
maxSum
.
Your goal is to return the maximum count of integers that can be chosen while satisfying all the above constraints.
Constraints:
banned.length
banned[i]
,n
maxSum
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Maximum Number of Integers to Choose From a Range I
How many integers can you select if banned
= [5, 10, 15, 20, 25], n
= 50, and maxSum
= 15?
1
2
3
4
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding on how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
import java.util.Arrays;public class Solution{public static int maxCount(int[] banned, int n, int maxSum) {// Replace this placeholder return statement with your codereturn -1;}}
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.