Median Marks

Hard
0/120
Average time to solve is 45m
profile
Contributed by
3 upvotes
Asked in company
Goldman Sachs

Problem statement

There are 'N' students in a class, where 'N is odd'. The 'ith' student wants their marks to be in the range 'L[i]' to 'R[i]' (both inclusive). The teacher can give a total of at most 'X' marks to students. To show that the result is good, the teacher wants the median marks to be as large as possible.

Return the maximum median marks the teacher can obtain provided they optimally distribute the 'X' marks among students satisfying the ranges.

To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting.

It is guaranteed that the teacher has enough marks to give, i.e. L[1]+L[2]+.....+L[N] <= 'X'.

For Example:-
Let 'N' = 3, 'X' = 13, 'L' = [2, 3, 5], 'R' = [4, 5, 7].
We can assign 2 marks to the first student, 5 marks to the second student, and 5 marks to the third student.
So the median mark is 5 which is the maximum possible. 
Detailed explanation ( Input/output format, Notes, Images )
Input Format:-
First-line contains an integer 'T', which denotes the number of test cases.
For every test case:-
First-line contains two integers 'N' and 'X'.
Second-line contains 'N' space-separated integers, the elements of the array 'L'
Third-line contains 'N' space-separated integers, the elements of the array 'R'.
Output Format:-
For each test case, Return the maximum median marks you can obtain.
Note:-
You don’t need to print anything. Just implement the given function.
Constraints:-
1 <= 'T' <= 10
1 <= 'N' <= 10^5
1 <= 'L[i]' <= 'R[i]' <= 10^5
1 <= 'X' <= 10^8
'N' is odd.

The Sum of 'N' overall test cases does not exceed 10^5.
Time Limit: 1 sec
Sample Input 1:-
2   
3 26
10 1 4
12 4 11
1 15
2
9
Sample Output 1:-
11
9
Explanation of sample input 1:-
First test case:-
We can assign 12 marks to the first student, 2 marks to the second student, and 11 marks to the third student.
So the median mark is 11 which is the maximum possible. 

Second test case:-
We can assign 9 marks to the first student.
So the median mark is 9 which is the maximum possible.
Sample Input 2:-
2
5 26    
4 2 2 6 5
4 4 7 8 6
1 5
4
7
Sample Output 2:-
6
5
Hint

Is the distribution monotonic in nature?

Approaches (1)
Binary Search, Greedy, Sorting.

Approach:-
 

  • Assign ‘left’= 1 and ‘right’= 10^5. While doing a binary search, we find ‘mid’ from ‘left’ and ‘right’, and for every ‘mid’ we check if it is possible to get the median equal to 'mid' or not.
  • If it is possible then we store ‘mid’ in our answer and assign left to mid+1, otherwise assign right to mid-1.
  • For checking, we can call the 'helper' function.
  • In helper function: -
  • We can divide the students into three groups.
    • R[i] < mid.
    • L[i] >= mid.
    • L[i] < mid <= R[i].
  • In order for the median marks to be at least 'mid', there must be at least (N+1)/2  marks greater than or equal to 'mid'. Let's denote the number of such marks as 'count'.
  • The first group can't increment the value of 'count' so it is beneficial to give minimum marks to this group.
  • The second group always increments the value of 'count', so it is beneficial to give minimum marks to this group.
  • Now for the third group, we can give 'mid' marks to max(0, ((N+1)/2 - 'count')) students and for the remaining student, we can give L[i] marks.

 

 Algorithm:-

 

  • Initialize variables 'ans' with 1, 'left' with 1, 'right' with 10^5.
  • while(left <= right) :
    • mid=(left+right)/2;
    • If (helper(mid, N, X, L, R) == 1) :
      • 'ans' = 'mid'.
      • 'left' = 'mid'+1.
    • Else :
      • right' = 'mid'-1.
  • Return 'ans' which is the required answer.
     

=> helper function:- 

  • Creating 'totalMarks', 'count', and 'extra'.
  • Iterate 'i' from 0 to 'N'-1:
    • If(R[i] < mid):
      • totalMarks+=L[i];
    • Else if (L[i]>=mid):
      • totalMarks+=L[i];
      • count++;
    • Else:
      • extra.push_back(L[i]);
  • Now in extra, we can give 'mid' marks to max(0, ((N+1)/2 - 'count')) students and for the remaining student, we can give L[i] marks, And also add the marks we give to 'totalMarks'.
  • If (totalMarks <= X):
    • Return 1;
  • Else :
    • Return 0;

 

Time Complexity

O(N*log(N)), where 'N' is the number of students.

 

We are performing a binary search that has time complexity O(log(N)) and also iterating in 'A' which has time complexity O(N), So the overall time complexity is O(N*log(N)).

Space Complexity

O(N), where 'N' is the number of students.

 

We are creating an array 'extra' which contains 'N' elements, So the Space Complexity is O(N).

Code Solution
(100% EXP penalty)
Median Marks
Full screen
Console