A student is taking a test on n different subjects. For each nth subject they have already answered answered[i] questions and have time to answer a total of q more questions overall. For each nth subject, the number of questions answered has to be at least needed[i] in order to pass. Determine the maximum number of subjects the student can pass if the additional answered questions are optimally distributed among the subjects.
For example, there are n = 2 subjects and needed = [4, 5] answered questions, respectively, to pass. The student has answered answered = [2, 4] questions in the two subjects so far, and can answer another q = 2 questions in all subjects combined. The best outcome is to answer an additional question in the second subject to pass it, and it is not possible to pass the first subject. The maximum number of subjects that can be passed is 1.
Complete the function maxSubjectsNumber in the editor below. The function must return an integer that represents the maximum number of subjects that can be passed.
maxSubjectsNumber has the following parameter(s):
answered[answered[0],...answered[n-1]]: an array of integersneeded[needed[0],...needed[n-1]]: an array of integersq: an integer
ദ്ദി(˵ •̀ ᴗ - ˵ ) ✧ Tons of thanks to the sunshine ~ spike~!
answered = [24, 27, 0] needed = [51, 52, 100] q = 100 return = 2
answered = [2, 4, 0] and needed = [5, 7, 100]. The additional answers needed to pass are [3, 3, 100]. The best distribution is at least 7 - 2 = 5 questions among the first two subjects. It would take all 100 questions to pass the third subject.answered = [24, 27, 0] needed = [51, 52, 100] q = 200 return = 3
answered = [2, 4] needed = [4, 5] q = 1 return = 1
1 ≤ n ≤ 10^50 ≤ answered[i], needed[i] ≤ 10^9
- Parent Process NumberOA · Seen Jun 2026
- Request Retry CountOA · Seen Jun 2026
- Count Ideal NumbersOA · Seen Jun 2026
- Count Descending SubarraysOA · Seen Apr 2026
- Count Power Products in RangeOA · Seen Apr 2026
- Minimum Operations to Make Alternating Binary StringSeen Feb 2026
- Minimum Number of Non-Empty Disjoint SegmentsSeen Feb 2026
- Count Unstable ProcessesOA · Seen Feb 2026
public int maxSubjectsNumber(int[] answered, int[] needed, int q) {
// write your code here
}