
Let ‘N’= 5, ‘ranges’ be [(5, 7), (6, 8), (3, 6), (10, 11), (15, 15)], ‘M’ = 3 and these 3 queries are given by array ‘queries’ = [4, 2, 8].
Then the sequence formed by distinct integers present between these ranges in ascending order is [3, 4, 5, 6, 7, 8, 10, 11, 15].
So,
The 4th smallest element of this sequence is 6.
The 2nd smallest element of this sequence is 4.
The 8th smallest element of this sequence is 11.
If the sequence has less than K integers then the answer will be -1.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The description of ‘T’ test cases follows-:
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ representing the number of ranges and number of queries respectively.
Next ‘N’ lines of the test case contain two space-separated integers, ‘start’ and ‘end’, denoting the starting and the ending point of the ith range.
The last line of the test cases contains ‘M’ space-separated integers representing ‘M’ queries.
For each test case, print a line consisting of M space-separated integers where the ith integer represents the answer of the ith query.
You do not need to print anything, it has already been taken care of. Just implement the given function.
In the given function, you need to return an array consisting of ‘M’ integers where the ith integer is the answer of the ith query. In a given function the parameter ‘ranges’ is the list of ‘N’ lists of 2 integers representing ‘N’ ranges where the 1st element of each list represent 'start' and the second element represent 'end' of the range, and parameter ‘queries’ which is the list of ‘M’ Integers representing queries.
1 <= T <= 50
1 <= N <= 10^4
1 <= M <= 10^4
1 <= K <= 10^9
1 <= start <= end <= 10^9
Where ‘T’ is the total number of test cases, ‘N’ is the number of ranges, ‘M’ is the number of queries, ‘K’ is the integer in the query, and ‘start’ and ‘end’ represent the first and the last integer of ranges,
Time Limit: 1 sec
Appraoch: We will create a list ‘sequence’ and push all distinct elements of these ranges in it. We then sort this list in ascending order. After that, each of the ‘M’ queries can be answered in constant time.
Approach: First, we need to sort the ranges based on lower limits in non-decreasing order and if the lower limits of ranges are the same, sort them according to higher limits in non-decreasing order.
Now we need to merge overlapping intervals. This can be done easily by iterating over the sorted list of ranges and checking whether the ‘start’ of the range is smaller or equal to the maximum value of ‘end’ of all the ranges before it.
After that, we can answer each of the M queries by simply iterating over the list of given ranges.
Approach: First, we need to sort the ranges based on lower limits in non-decreasing order and if the lower limits of ranges are the same, sort them according to higher limits in non-decreasing order.
Now we need to merge overlapping intervals. This can be done easily by iterating over the ranges and checking whether the ‘start’ of the range is smaller or equal to the maximum value of ‘end’ of all the ranges before it.
After that, for each range, we find out what will be the position of ‘start’ and ‘end’ of that range in the sequence formed by arranging all distinct elements of these ‘N’ ranges in ascending order. Queries can be answered using binary search after this.
Corporate Skill Diversity
Student Percentile Calculation
Player Leaderboard Sorting
Minimized Maximum of Products Distributed to Any Store
K-Distinct Subarray Sum