
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.
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.
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.
You do not need to print anything. It has already been taken care of. Just implement the given functions.
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.
A disjoint-set data structure is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. A union-find algorithm is an algorithm that performs two useful operations on such a data structure:
Group Points
Group Points
Group Points
Group Points
Check Equations
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Good Bad Graph
Good Bad Graph
COUNT ISLANDS