Update appNew update is available. Click here to update.

Data Stream As Disjoint Intervals

Contributed by
Yash_5830
Last Updated: 23 Feb, 2023
Medium
yellow-spark
0/80
Avg time to solve 30 mins
Success Rate 70 %
Share
1 upvotes

Problem Statement

You are given a stream of 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:

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}}.
Detailed explanation ( Input/output format, Notes, Images )
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
Reset Code
Full screen
Auto
copy-code
Console