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 523753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:29:42+00:00 2026-05-13T08:29:42+00:00

I have got a series of time intervals (t_start,t_end), that cannot overlap, i.e.: t_end(i)

  • 0

I have got a series of time intervals (t_start,t_end), that cannot overlap, i.e.: t_end(i) > t_start(i+1). I want to do the following operations:

1) Add new (Union of) intervals [ {(1,4),(8,10)} U (3,7) = {(1,7),(8,10)} ]
2) Take intervals out [ (1,7) – (3,5) = {(1,3),(5,7)}
3) Checking whether a point or a interval overlaps with an interval in my series (intersection)
4) Finding the first “non-interval” of a minimum length after some point [ {(1,4),(7,8)}: there is a “non-interval” of length 3 between 4 and 7 ].

I want to know good ways of implementing this, with low complexities (log n for all operations would do it).

Related question: Data structure for quick time interval look up

  • 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-13T08:29:43+00:00Added an answer on May 13, 2026 at 8:29 am

    It sounds like you could just use a balanced binary tree of all the boundary times.

    For example, represent {(1,4), (8,10), (12,15)} as a tree containing 1, 4, 8, 10, 12, and 15.

    Each node needs to say whether it’s the start or end of an interval. So:

                              8 (start)
                             /        \
                    1 (start)         12 (start)
                          \             /      \
                         4 (end)   10 (end)   15 (end)
    

    (Here all the “end” nodes ended up at the bottom by coincidence.)

    Then I think you can have all your operations in O(log n) time. To add an interval:

    • Find the start time. If it’s already in the tree as a start time, you can leave it there. If it’s already in the tree as an end time, you’ll want to remove it. If it’s not in the tree and it doesn’t fall during an existing interval, you’ll want to add it. Otherwise you don’t want to add it.

    • Find the stop time, using the same method to find out if you need to add it, remove it, or neither.

    • Now you just want to add or remove the abovementioned start and stop nodes and, at the same time, delete all the existing nodes in between. To do this you only need to rebuild the tree nodes at or directly above those two places in the tree. If the height of the tree is O(log n), which you can guarantee by using a balanced tree, this takes O(log n) time.

    (Disclaimer: If you’re in C++ and doing explicit memory management, you might end up freeing more than O(log n) pieces of memory as you do this, but really the time it takes to free a node should be billed to whoever added it, I think.)

    Removing an interval is largely the same.

    Checking a point or interval is straightforward.

    Finding the first gap of at least a given size after a given time can be done in O(log n) too, if you also cache two more pieces of information per node:

    • In each start node (other than the leftmost), the size of the gap immediately to the left.

    • In every node, the size of the largest gap that appears in that subtree.

    To find the first gap of a given size that appears after a given time, first find that time in the tree. Then walk up until you reach a node that claims to contain a large enough gap. If you came up from the right, you know this gap is to the left, so you ignore it and keep walking up. Otherwise you came from the left. If the node is a start node, check to see if the gap to its left is large enough. If so, you’re done. Otherwise, the large-enough gap must be somewhere to the right. Walk down to the right and continue down until you find the gap. Again, because the height of the tree is O(log n), walking it three times (down, up, and possibly down again) is O(log n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a time series in R. I want to construct a matrix, where
I've got a series of SVG files that have been created in inkscape (or
Have got an NSString *str = @12345.6789 and want to find out if there
I got this an asp.net 3.5 page that and I have few tabs (telerik
I have a series of functions that are all designed to do the same
I have got an MS chart that I am showing in an ASP.net page.
I got a application where I use DateTime.MaxValue to indicate that a entry have
Say I have got some data series generated by my plotting application, and I
I've got a time series of observations of 5 variables y, x_1, x_2, x_3,
I've got some data in JSON format that I want to do some visualization

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.