0

# Data Stream As Disjoint Intervals

Difficulty: MEDIUM
Contributed By
Yash Bansal
Avg. time to solve
30 min
Success Rate
70%

Problem Statement

#### 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:

``````Let's say we have an array of integers {1, 3, 4}. The disjoint intervals for the given array will be {{1,1} , {3,4}}.
``````
##### Input format:
``````The first line of input contains an integer ‘T’, denoting the number of test cases.

The first line of each test case 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 only, output the answer to the query in a single line.

The output for each test case will be printed on a separate line.
``````
##### Note:
``````You do not need to print anything. It has already been taken care of. Just implement the given functions.
``````
##### Constraints:
``````1 <= T <= 10
1 <= N <= 3 * 10 ^ 4
0 <= VAL <= 10 ^ 9

Where ‘T’ represents the number of test cases, ‘N’ represents the number of queries, and ‘VAL’ represents the integer that has to be added to the stream.

Time Limit: 1 sec.
``````
##### Sample Input 1:
`````` 2
6
1 1
2
1 3
2
1 2
2
6
1 3
2
1 6
2
1 5
2
``````
##### Sample Output 1:
`````` 1 1
1 1 3 3
1 3
3 3
3 3 6 6
3 3 5 6
``````
##### Explanation 1:
``````For the first test case,
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} merges together so that all the integers added to the stream so far comes in this interval.

For the second test case,
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.
``````
##### Sample Input 2:
``````2
6
1 1
2
1 4
2
1 3
2
4
1 4
2
1 9
2
``````
##### Sample Output 2:
``````1 1
1 1 4 4
1 1 3 4
4 4
4 4 9 9
``````
Console