0

Need algorithm or solution to check if multiple range overlap in between with each other! This range can be dynamic. For example:

//Dynamic Range

var a = [ [1, 2], [3, 4], [2, 3], [5, 6] ];

So, in above array given, first array value of "a" has 1 and 2 as values, here, 2 comes in 3rd array value [2,3] so this should give false and so on...

Language preferred: Javascript

Any help will be appreciated!

1
  • Algorithm recommendations are off topic on this site. Commented Sep 30, 2020 at 13:05

1 Answer 1

3

You could just use double some methods and then uses those start and end intervals to check of overlap.

const arr = [[1, 2], [3, 4], [2, 3], [5, 6]];

const overlap = arr.some(([sA, eA], i) => {
  return arr.slice(i + 1).some(([sB, eB]) => {
    return sB >= sA && sB <= eA || eB >= sA && eB <= eA ||
      sA >= sB && sA <= eB || eA >= sB && eA <= eB
  })
})

console.log(overlap)

Sign up to request clarification or add additional context in comments.

Comments

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.