Ninja is assigned some task to complete in the given intervals. Each interval has a starting point and ending point and is given in the form of a list. But there is an issue with these intervals as some intervals starting point and ending points lie in some other intervals so ninja has to remove such intervals and wants to know the count of remaining intervals.
So your task is to detect such intervals and return the count of remaining intervals in the list after removing such overlapping intervals.
Example:
Let assume the given interval list is ‘[ [ 1, 4 ], [ 5, 10 ], [ 6, 9 ] ] so we return ‘2’ as our answer as the interval [ 6, 9 ] lies in the interval [ 5, 10 ] so we removed [ 6, 9 ] from the list hence remaining intervals left are ‘2’ so ‘2’ is our answer.
Note:
We can take two intervals even when their starting and ending time are same. For eg: [1,3], [3,5] can be taken together.
Input Format:
The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains an integer ‘N’ denoting the size of the given interval list. Then, ‘N’ lines follow.
Each line contains two space-separated integers denoting the starting and ending points of the interval.
Output Format:
For each test case, print a single line containing the remaining intervals left in the list after removing intervals that lie in other intervals.
The output of each test case will be 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 <= 5
1 <= N <= 3000
0 <= arr[i][0], arr[i][1] < 10 ^ 9
Where ‘T’ is the number of test cases, ‘N’ is the size of the given list or we can say an array of array, and ‘arr[i][0]’ and ‘arr[i][1]’ represents the starting and ending points of the intervals.
Time Limit: 1 sec
2
3
1 4
5 10
6 9
3
1 10
2 6
3 8
2
1
Test Case 1:
For the first test case, the list is [ [ 1, 4 ], [ 5, 10 ], [ 6, 9 ] ].
So we can see that interval [ 6, 9 ] lies in the interval [ 5, 10 ] so we will remove that interval. Hence the remaining intervals left are ‘2’ so we return ‘2’ as our answer.
Test Case 2:
For the second test case, the list is [ [ 1, 10 ], [ 2, 6 ], [ 3, 8 ] ]
So we can see that both [ 2, 6 ] and [ 3, 8 ] lie in the interval [ 1, 10 ] so we removed both the intervals hence the remaining interval is [ 1, 10 ] so we return ‘1’ as our answer.
1
3
2 4
3 4
4 5
2
For this test case, the list is [ [ 2, 4 ], [ 3, 4 ], [ 4, 5 ] ] so we can see that both [ 2, 4 ] and [ 3, 4 ] have some common points so we remove any one of the interval. Hence remaining intervals left is [ 1, 5 ] and [2, 4] so we return ‘2’ as our answer.
Can you traverse the whole list and compare each interval?
The idea here is to use a brute force approach and compare each and every interval if any interval starting point is greater than or equal to any given interval starting interval and less than or equal to its ending interval we have to count that numbers.
Algorithm:
O(N ^ 2), where ‘N’ represents the size of the given array.
As for each index, we are traversing the whole list so we have to run two nested loops which make the complexity O(N ^ 2).
O(1),
As no extra space is required.