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

  • Home
  • SEARCH
  • 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 9147093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:54:16+00:00 2026-06-17T10:54:16+00:00

I am trying to figure out how to code in a clean way the

  • 0

I am trying to figure out how to code in a clean way the fact that a binary search returns the insertions point for a missing element.

I use this for the algorithmic problem of finding the maximum subset of non-overlapping intervals.

What I do is after sorting the intervals keep each interval the its start time does not overlap with the already selected intervals end time.

Although I could do a linear search, I thought that using a binary search would be better here. Am I right?

So I did the following that although it seems correct, it is error prone and I think there could be better usage of the APIs.

What I am doing is binary search on the end interval and then see if it overlaps with the previous or next (using the insertion point returned by binary search).

Is my logic correct? Also I believe this algorithm can be a good exercise so I am looking a clean java version.

private boolean nonOverlapping(Pair interval, SortedSet<Pair> selectedIntervals) {  
    if(selectedIntervals.isEmpty())  
        return true;  
    if(selectedIntervals.contains(interval)){  
        return true;  
    }       
    Pair[] sortedSelections = selectedIntervals.toArray(new Pair[0]);  
    int pos = Arrays.binarySearch(sortedSelections, interval, new Comparator<Pair>() {  

        @Override  
        public int compare(Pair o1, Pair o2) {  
            return o1.getStart() - o2.getEnd();   
        }  
    });  
    pos = (-pos) -1;  
    if(pos == sortedSelections.length){  
        if(sortedSelections[pos - 1].getEnd() < interval.getStart()){  
            return true;  
        }  

    }           
    else if(sortedSelections[pos].getEnd() > interval.getStart()){  
        if(pos + 1 < sortedSelections.length){  
            if(sortedSelections[pos + 1].getEnd() < interval.getStart()){  
                return false;   
            }   
        }  
        if(pos - 1 >= 0){  
            if(sortedSelections[pos - 1].getEnd() < interval.getStart()){  
                return false;  
            }  
        }  
        return true;  
    }  


    return false;  
}  
  • 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-06-17T10:54:17+00:00Added an answer on June 17, 2026 at 10:54 am

    The problem of finding the largest nonoverlapping subset of a range of intervals is a classic application of a greedy algorithm. The standard greedy algorithm is the following:

    1. Sort all of the intervals by their end time.
    2. Processing all intervals in order:
      1. Add the interval that ends at the earliest possible time.
      2. Remove all intervals that conflict with that interval.
    3. The resulting set of intervals is a maximum subset of nonoverlapping intervals.

    It should be clear from this algorithm that the resulting set does not contain any overlapping intervals, since each step of the algorithm removes all conflicting intervals from future consideration.

    To see that this is optimal, you can use an exchange argument to show that there cannot be a better solution. You can find details online, such as in this set of lecture slides.

    Hope this helps!

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

Sidebar

Related Questions

I'm trying to figure out a clean way of getting data back from my
I can't figure out a clean way to implement an algorithm that will work
I am trying to figure out the best way to parse a search results
I am trying to figure out a way with twitter's new guidelines that will
i'm trying to figure out the code for the jquery selector test found at
Trying to figure out this pseudo code. The following is assumed.... I can only
I'm trying to figure out why this code won't compile. I have interface A
I'm trying to figure out whether the code located after throw new Exception in
I'm trying to figure out how to use Emacs Code Browser (ECB) and one
I am trying to figure out how to simplify this piece of code. 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.