Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

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
Full screen
Console