Analog device provides instant result as you can see at this simplified picture. Is there known algorithm that solves the same problem when I have similar situation in digital world, with arrays of ranges? Or at least something fast, with least number of loops. It should print out available ranges that are not used in any other array.
Pseudo code or Python or PHP would be great.
Asked
Active
Viewed 52 times
2

Ibiza
- 149
- 4
-
hi, perhaps use the adc lib https://docs.micropython.org/en/latest/library/pyb.ADC.html – jspcal Dec 24 '22 at 01:10
-
1If you want to count the overlaps you could use a Fenwick tree. If you just want the places where there is no range, then you can keep inserting the ranges in a binary tree, if they touch or overlap you merge, so you are only left with a few ranges with gaps in between them and you can get those ranges directly in order from the constructed tree. – maraca Dec 24 '22 at 01:58
-
We need to know the format of your input. Intercalls or slots that are set/ not set? – MrSmith42 Dec 24 '22 at 16:36
-
Slots are not set. If it would benefit I could set them to 1 second, but it might take to much space. Input are arrays, like from above example c([4, 9], [59, 64]), f([5, 8], [18, 24], [50, 60]) – Ibiza Dec 26 '22 at 00:43
1 Answers
0
So you only want the gaps? I noticed my comment with binary tree is way too complicated. Here is a simple algorithm:
Flatten the ranges, so you end up with a single array/list with all the ranges inside.
Sort the ranges by their min, i.e. left point.
For each range do the following:
- Save the min and max of the current range.
- While the min of the next range is smaller or equal to the max: Set current to the next range (this range has to be skipped in the outer loop now, so it's easiest to have a loop with index) and if its max is bigger update max.
- Add a new range [min, max] to the result array/list
Now you have ranges with the covered areas in order and there is a gap between each range. So you also have all non-covered ranges in order, the result just needs to be interpreted differently: the min of a range becomes the max of the gap range and the max becomes the min of the next gap range, gap ranges that are missing min or max are open ranges (like this ranges are inclusive and gap ranges exclusive otherwise we need to add/subtract one from the max/min).

maraca
- 8,468
- 3
- 23
- 45