‘STUDENTS’ = [[2, 3], [5, 1], [1, 2], [4, 1]]
‘ANS’ = [[1, 2], [4, 1], [2, 3], [5, 1]]
Here, STUDENTS[i][0] and STUDENTS[i][1] represent the blue and black pens student ‘i’ have, respectively.
‘STUDENTS’ in terms of the total number of pens students have = [[5], [6], [3], [5]]
Arrange according to the requirement = [[3], [5], [5], [7]]. The fourth student will come before the first student as he has more blue pens.
Therefore, the final answer is [[1, 2], [4, 1], [2, 3], [5, 1]].
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case has an integer ‘N’ representing the size of ‘STUDENTS’.
The next ‘N’ lines have two single space-separated integers ‘BLUE’ and ‘BLACK’ which represent the blue and black pens ‘i-th’ student has, respectively.
For each test case, print ‘N’ lines each having two integers separated by a single space representing the blue and black pens that the ‘i-th’ student has, under the condition given in the description.
You don't need to print the output, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10^5
0 <= BLUE, BLACK <= 10^8
Time Limit: 1 sec
The basic idea is to sort the complete array on the basis of the total number of pens a student has. Please note that if two or more students having the same number of total pens then we are looking for the number of blue pens they have. Therefore, the comparator used to sort the array plays the most crucial role here.
Declaration of the ‘CMP’ (comparator) function:
bool cmp(vector<int> student1, vector<int> student2){ }Where ‘student1’ and ‘student2’ are two students.
The steps are as follows: