
You are given a stream of 'n' non-negative integers as the input, and you have to group the stream of integers in the form of disjoint intervals.
Your task is to Implement the ‘DisjointIntervals’ class having the two functions:
1) The first function is ‘addInteger(int val)’, which takes an integer ‘val’ as an argument and adds it to the stream.
2) The second function is ‘getDisjointIntervals()’, which returns a summary of the integers in the stream currently as a list of disjoint intervals.
Example:
Input: 'n' = 5 , stream = [
[1, 1],
[1, 3],
[2],
[1, 2],
[2],
]
Output: [
[ [1, 1], [3, 3] ],
[ [1,3] ]
]
Explanation: First of all, 1 is added to the stream, and the disjoint interval will be {1, 1}. When 3 will be added to the stream, then the disjoint intervals will be {1, 1}, {3, 3}. But when 2 is added to the stream then the disjoint interval will be {1, 3} as 2 lies between these two sets of disjoint intervals, and both the intervals {1, 1} and {3, 3} merge.
The first input line contains an integer ‘n’, representing the total number of queries.
Then the next ‘n’ lines contain ‘n’ queries. A query can be of two types:
1 val → adds the integer ‘val’ to the stream.
2 → returns a list of disjoint intervals.
Output format:
For each test case, print all the disjoint intervals for each query of type 2, and output the answer to the query in a single line.
Each query must be answered in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given functions.
6
1 3
2
1 6
2
1 5
2
3 3
3 3 6 6
3 3 5 6
First 3 is added to the stream, and the disjoint interval will be {3, 3}. When 6 will be added to the stream, then the disjoint intervals will be {3, 3},{6, 6}. But when 5 is added to the stream, then the disjoint interval will be {3, 3}, {5, 6} as 5 merges with the interval {6, 6} because the difference between interval {5, 5} and {6, 6} is less than 2.
6
1 1
2
1 4
2
1 3
2
1 1
1 1 4 4
1 1 3 4
First, 1 is added to the stream, and the disjoint interval will be {1, 1}. When 4 will be added to the stream, then the disjoint intervals will be {1, 1},{4, 4}. But when 3 is added to the stream, then the disjoint interval will be {1, 1}, {3, 4} as 3 merges with the interval {4, 4}.
1 <= 'n' <= 10 ^ 5
0 <= 'val' <= 10 ^ 4
Time Limit: 1 sec.
Can you solve this problem by using linear search?
In this approach, for any new number ‘Val’, we will find the position of the first interval whose starting value is greater than the added ‘Val’ to the stream using linear search.
O(N ^ 2), where ‘N’ is the total number of queries.
Since there are ‘N’ queries and for each of these queries, we will either call the ‘addInteger’ function, which we take O(N) time for performing a linear search for each of the integer added to the stream giving time complexity of O(N) per query, or the ‘getDisjointIntervals’ functions which will take O(N) time to get all the required intervals. So overall time complexity will be O(N ^ 2).
O(1).
We are using only Constant extra space. So overall space complexity will be O(1).