Given a infinite stream of intergers, construct the list of pairs where each pair represents all the integer elements between the range of two elements. Maintain the updated List of pairs. Print/Output the updated list after each encounter of an integer in the stream
Stream till now : 7,10,15,9,8,3,2,5,…
Output of list till now :
[[7,10],[15,15],[2,5]]
say there is integer 6 in coming as next element in the stream:
Updated List of pairs or Array of pairs would be:
[[2,10],[15,15]]
Note: If a pair is [1,4] then you have all the integers [1,2,3,4] from the stream, [9,13] represents [9,10,11,12,13] etc, else you have to add a self element pair into the list like [x,x] if number x doesn’t fall under a range in contructed pairs of your existing list. Update and output the list of pairs as you read the stream of integers.
I know, the question is bit unclear and confusing. I hope the below detailed example will clarify and make the question simpler.
For Example:
Stream of integers […]:
1,…
Updated List : [[1,1]] -> as there is only one element in the stream now.
1,3,…
List : [[1,1] , [3,3]] -> there is no 2 between 1 and 3 number till now so 3 would have separate self pair.
1,3,2,…
List: [[1,3]] -> as 2 arrived in the stream, list gets updated to 1,3 as it includes 1,2,3 [ all the elements in between the range], discarded the [3,3] pair.
1,3,2,7,…
List : [[1,3],[7,7]] -> similary after integer 3 and before 7 there are missing elements.
1,3,2,7,9,…
List : [[1,3],[7,7],[9,9]]
1,3,2,7,9,5,…
List : [[1,3],[7,7],[9,9],[5,5]]
1,3,2,7,9,5,6,…
List: [[1,3],[5,7],[9,9]]
1,3,2,7,9,5,6,4,…
List: [[1,7],[9,9]]
1,3,2,7,9,5,6,4,8,…
List : [[1,9]]
and so on…