0

If I have ranges of numbers and I need to return the sets that do not overlap with one another For example, here is a set of numbers:

1,4
0,3
4,7
0,4

I need to return:

0,3
4,7

An example of a larger data set:

0,1
0,2
2,5
4,8
3,7
8,11
8,9
9,11

I would need to return:

0,2 
3,7 
8,11

It would need to account for situations where if a smaller set is in another and should be discarded. Using the previous set,

0,1
0,2
3,7

Since 0,1 is contained within 0,2 set 0,1 should be discarded.

Any help would be appreciated. Thanks much.

2
  • What do you mean 'contained within'? Commented May 13, 2016 at 19:36
  • 1
    if one range is 0-2 [0,1,2] and the other range is 0-1 [0,1], 1 is between 0-2 and therefore 0,1 should be discarded. Does that help? Commented May 13, 2016 at 20:00

1 Answer 1

1

If your goal is to find the largest set of non-overlapping sets you should check Interval scheduling problem, especially solution using greedy algorithm. If performance doesn't matter you can try playing with python set union or subset and compare sets with each other.

set(range(0, 4)).union(set(range(0, 1)))
Sign up to request clarification or add additional context in comments.

3 Comments

The goal is to find the largest non-overlapping sets within a group that would sit next to each other. For example, 0,4 and 5,8 and 9,11 so that set 3,10 would be discarded.
You might want to play with recursive functions to find ones that sit next to each other. Not sure quite how to do it though.
Thanks for referring me to interval scheduling. It is what I was looking for, I was able to modify the following code to get the result needed: github.com/farazdagi/algorithms/blob/master/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.