Maximum Subjects Number

A student has already answered, answered[i] questions in each of the ith subjects, and still has time to answer a total of q more questions overall. For each ith 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 q 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 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.

Function Description

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 integers
  • needed[needed[0],…needed[n-1]]: an array of integers
  • q: an integer
Constraints
  • 1 <= n <= 10^5
  • 0 <= answered[i], needed[i],q <= 10^5
Input Format
The first line contains an integer, n, the number of elements in answered, and the number of subjects.
Each of the next n lines contains one integer, answered[i], the number questions already answered for the ith subject.
The next line contains the integer, needed[i], the number of questions answered needed to pass the ith subject.
The next line contains an integer, q, the total number of additional questions the student can answer across all the subjects.
Output Format
An integer represents the maximum number of subjects that can be passed.
Sample Input1
3
24
27
0
51
52
100
100
Sample Output1
2
Explanation:

Here answered = [24, 27, 0] and needed = [51, 52, 100]. The additional answers needed to pass are [27, 25, 100]. The best distribution is at least 27 + 25 = 52 questions among the first two subjects. It would take all q = 100 questions to pass the third subject.

Sample Input2
3
27
0
3
51
52
100
200
Sample Output2
3
Explanation:

Here answered = [27, 0, 3] and needed = [51, 52, 100]. The additional answers needed to pass are [24, 52, 97]. With q = 200 questions that can be answered, there are enough answers to pass all three subjects. Answer at least the minimum questions needed per subject, and distribute the 200 – (24 + 52 + 97) = 27 remaining questions at random.

C JAVA PYTHON
#include<stdio.h>

int maxSubjectsNumber(int,int[],int[],int);

int main(){
  int n, answered[50], needed[50], q;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d",&answered[i]);
  }
  for(int i=0;i<n;i++){
    scanf("%d",&needed[i]);
  }
  scanf("%d",&q);
  printf("%d",maxSubjectsNumber(n,answered,needed,q));
  return 0;
}

int maxSubjectsNumber(int n, int answered[], int needed[], int q){
  int count = 0;
  for(int i=0;i<n;i++){
    if(answered[i]<needed[i] && q>=(needed[i]-answered[i])){
      q = q - (needed[i]-answered[i]);
      count++;
    }
  }
  return count;
}
Sample Input
3
27
0
3
51
52
100
200
Sample Output
3