Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3595260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:49:44+00:00 2026-05-18T19:49:44+00:00

Say I have an array of arrays in Ruby, [[100,300], [400,500]] that I’m building

  • 0

Say I have an array of arrays in Ruby,

[[100,300], 
 [400,500]]

that I’m building by adding successive lines of CSV data.

What’s the best way, when adding a new subarray, to test if the range covered by the two numbers in the subarray is covered by any previous subarrays?

In other words, each subarray comprises a linear range (100-300 and 400-500) in the example above. If I want an exception to be thrown if I tried to add [499,501], for example, to the array because there would be overlap, how could I best test for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:49:44+00:00Added an answer on May 18, 2026 at 7:49 pm

    Since your subarrays are supposed to represent ranges, it might be a good idea to actually use an array of ranges instead of an array of array.

    So your array becomes [100..300, 400..500].

    For two ranges, we can easily define a method which checks whether two ranges overlap:

    def overlap?(r1, r2)
      r1.include?(r2.begin) || r2.include?(r1.begin)
    end
    

    Now to check whether a range r overlaps with any range in your array of ranges, you just need to check whether overlap?(r, r2) is true for any r2 in the array of ranges:

    def any_overlap?(r, ranges)
      ranges.any? do |r2|
        overlap?(r, r2)
      end
    end
    

    Which can be used like this:

    any_overlap?(499..501, [100..300, 400..500])
    #=> true
    
    any_overlap?(599..601, [100..300, 400..500])
    #=> false
    

    Here any_overlap? takes O(n) time. So if you use any_overlap? every time you add a range to the array, the whole thing will be O(n**2).

    However there’s a way to do what you want without checking each range:

    You add all the ranges to the array without checking for overlap. Then you check whether any range in the array overlaps with any other. You can do this efficiently in O(n log n) time by sorting the array on the beginning of each range and then testing whether two adjacent ranges overlap:

    def any_overlap?(ranges)
      ranges.sort_by(&:begin).each_cons(2).any? do |r1,r2|
        overlap?(r1, r2)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an array that has 4 arrays in it. What I
In Ruby 1.8.6, I have an array of, say, 100,000 user ids, each of
Say I have an array of arrays in Ruby, array = [[bob, 12000, broke,
Say I have an array that looks like this: array(100) { [0]=> array(4) {
Let's say I have a Ruby array a = [1, 2, 3, 4] If
Let's say I have an array of integers in Ruby: a = [1, 4,
Say we have array of arrays: tree_limbs = Array.new tree_limbs << %w(1 2 3
I have four arrays that are coming in from the client. Let's say that
Let's say I have an Array that contains more than three repetitions of a
Lets say i have an array of arrays, of which i dont know the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.