Input: ‘N’ = 3, ‘L’ = 2, ‘ARR’ = [(1, 1), (5, 3), (8, 2)].
Output: [(1, 1), (8, 2), (5, 3)].
The last values of each type are (1, 3, 2). Sorting them in non-decreasing order we get (1, 2, 3). Hence the final result is [(1, 1), (8, 2), (5, 3)].
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains two integers ‘N’ and ‘L’ where ‘N’ denotes the length of the array ‘NUMS’ and ‘L’ denotes the length of the tuples.
The next ‘N’ lines of each test case contain ‘N’ tuples of integers.
For each test case, you don’t need to return anything just return the resultant array.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
Sum of total number of integers <= 10^5
1 <= ARR[i].length <= 1000
Time Limit: 1 sec
In the naive approach, we can use bubble sort. Iterate over each element of the array then run a second loop from the starting element and swap the adjacent tuples if the last element of the first tuple is greater than the last element of the second tuple.
In this approach, we can modify the already existing in-built sorting function( stable_sort ) by writing our own compare function. Declare a function compare which takes two tuples as arguments and compares them according to their last element.