
You are given an array of intervals, where intervals[i] = [start(i), end(i)] and each start(i) is unique.
The smart interval for an interval ‘i’ is an interval ‘j’ such that start(j) is greater than or equal to end(i) and start(j) should be minimum.
Your task is to return an array of smart interval indices for each interval. If no smart interval exists for an interval ‘i’, then place -1 at index ‘i’ in the output array.
Input Format:
The first line contains a single integer T representing the number of test cases.
The first line of the test case contains an integer ‘N’ denoting the number of intervals.
Each of the following ‘N’ lines contains 2 space-separated integers denoting the start and end of an interval.
Output Format :
The first and only line of output contains ‘N’ space-separated integers as described above.
The output of every test case is printed in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^4
1 <= start, end <= 10^4
Time Limit : 1sec
1
3
1 2
2 3
3 4
1 2 -1
Smart interval for the interval [1, 2] is [2, 3] because the start point of [2, 3] is equal to the end point of [1, 2]. The index of [2, 3] is 1.
Smart interval for the interval [2, 3] is [3, 4] because the start point of [3, 4] is equal to the end point of [2, 3]. The index of [3, 4] is 2.
Smart interval for the interval [3, 4] does not exist. Hence, -1.
Therefore, the final answer is [1, 2, -1].
2
2
3 2
2 3
1
1 1
1 0
0
Test Case 1:
Smart interval for the interval [3, 2] is [2, 3]. The index of [2, 3] is 1.
Smart interval for the interval [2, 3] is [3, 2]. The index of [3, 2] is 2.
Hence, the answer is [1, 0].
Test Case 2:
Do you really need an explanation?
Check for all the possibilities using brute force.
The idea is to check all the intervals for a particular interval and store the index of the smart interval for the current interval. This can be done using two nested loops.
Algorithm:
O(N ^ 2), where N is the number of intervals.
For each interval in the given intervals, we are iterating over all the intervals. Thus, the total time complexity is O(N) * O(N) = O(N ^ 2).
O(N), where N is the number of intervals.
O(N) space is used by the output array ‘result’.