FastPrepFastPrep
Problem Brief

Math Homework

FULLTIMENEW GRADOA

You are given a sorted array points representing the point values of homework problems in the order they appear.

A student must solve the first problem at index 0. After solving a problem at index i, the student may solve either the next problem i + 1 or skip one problem and solve i + 2.

The student continues until the difference between the maximum and minimum point values among the solved problems is at least threshold. If that can never be achieved, the student must solve all problems.

Return the minimum number of problems the student needs to solve.

Function Description

Complete the function minProblemsToSolve in the editor below.

minProblemsToSolve has the following parameters:

  • int[] points: sorted point values for the homework problems
  • int threshold: the required spread between the minimum and maximum solved point values

Returns

int: the minimum number of problems that must be solved

1Example 1

Input
points = [1, 2, 3, 5, 8], threshold = 4
Output
3
Explanation
Solve the problems worth 1, 3, and 8. The solved values have spread 8 - 1 = 7, and it takes only 3 problems.

2Example 2

Input
points = [1, 2, 3, 4, 5], threshold = 4
Output
3
Explanation
Solve the problems worth 1, 3, and 5. The spread is exactly 4, so the answer is 3.

3Example 3

Input
points = [4, 5, 6, 7], threshold = 10
Output
4
Explanation
Even after solving every problem, the spread is only 7 - 4 = 3, so the student must solve all 4 problems.

Constraints

Limits and guarantees your solution can rely on.

  • points is sorted in non-decreasing order.
  • 1 ≤ points[i] < 1000.
  • 1 ≤ threshold < 1000.
public int minProblemsToSolve(int[] points, int threshold) {
  // write your code here
}
Input

points

[1, 2, 3, 5, 8]

threshold

4

Output

3

Sign in to submit your solution.