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.

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:

  1. The selected integers must fall within the range [1,n][1, n].

  2. Each integer can be chosen at most once.

  3. No selected integer can be present in the banned array.

  4. 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:

  • 11 \leq banned.length 103\leq 10^3

  • 11 \leq banned[i], n 103\leq 10^3

  • 11 \leq maxSum 106\leq 10^6

Examples

Press + to interact
canvasAnimation-image
1 of 4

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

1

How many integers can you select if banned = [5, 10, 15, 20, 25], n = 50, and maxSum = 15?

A)

1

B)

2

C)

3

D)

4

Question 1 of 30 attempted

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.

Drag and drop the cards to rearrange them in the correct sequence.

Try it yourself

Implement your solution in the following coding playground. 

Press + to interact
Java
usercode > Solution.java
import java.util.Arrays;
public class Solution{
public static int maxCount(int[] banned, int n, int maxSum) {
// Replace this placeholder return statement with your code
return -1;
}
}
Maximum Number of Integers to Choose From a Range I

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.