
Here, Disjoint ranges mean that no two ranges from the given list intersect each other.
A list of ranges [ (X1, Y1), (X2, Y2), (X3, Y3) … (Xn, Yn) ] is sorted if and only if Xi > Xi-1 and as the given ranges are disjoint, so Xi > Yi-1 for each i [ 1 < i <= n].
Let ‘N’= 3, given list be [(1, 3), (4, 6), (8, 11) ], and ‘removeRange’ be (2, 8).
Then after removing all intersections between ranges in the list and ‘removeRange’.The list will look like [(1, 2), (8, 11) ].
As we have removed,
Range (2,3) from the first range (1,3).
The whole second range (4, 6) as this is fully intersecting with the given ‘removeRange’
Nothing from the third range (8, 11) as no point is in this range is intersecting with the given ‘removeRange’ (2,8).
The resulting list after removing ranges can be empty also.
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 an integer ‘N’ representing the number of ranges in the given list.
Next ‘N’ lines of the test case contain two space-separated integers, ‘start’ and ‘end’, denoting the starting and the ending point of the i-th range.
The last line of the test cases contains two space-separated integers, ‘start’ and ‘end’, denoting the starting and the ending point of the ‘removeRange’.
For each test case, print ‘K’ lines containing two space-separated integers, ‘start’ and ‘end’, denoting the starting and the ending point of the ith range in the resulting list after removing ranges. Where ‘K’ is the size of the resulting list of ranges. If ‘K’ is 0 i.e. the resulting list does not contain any range then print ‘-1’
The output of each test case will be printed in a separate line.
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 a 2D array ‘arr[K][2]’, where ‘K’ is the number of ranges in the resulting list after removing ranges and ‘arr[i][0]’, ‘arr[i][1]’ are the starting and ending points in the ‘i th’ range of resulting string.
1 <= T <= 5
1 <= N <= 3000
1 <= start <= end <= 10^9
Where ‘T’ is the total number of test cases, ‘N’ is the number of ranges, and ‘start’ and ‘end’ represent the first and the last integer of a range.
Time Limit : 1 sec
The idea here is to iterate over all points from minimum to maximum point in the given list of ranges and check if this point can be part of the range in the result list. The minimum point will be the ‘start’ point of the first range in the list and the maximum point will be the ‘end’ point of the last range in the given list because the given list is sorted. A point can only be included in the answer if and only if it lies in one of the given ranges and doesn’t lie on the given ‘removeRange’, else it cannot be included in the answer.
The function will take two parameters:
For any range in the given list, there can be only one of the four below case
All these cases can be handled easily as shown in the below figure. (red line denotes the given ‘removeRange’).
