Question

In Java, if I have two intervals....interval A (1-5) and interval B (3-10)...how can i effectively...

In Java, if I have two intervals....interval A (1-5) and interval B (3-10)...how can i effectively use binary search to not only show the intervals but to also show the overlap which in this case is (3-5).

Homework Answers

Answer #1

You can do this by modified versio of binary search

Let interval is [x, y]

1) Use Binary search to get index of the first occurrence of x in arr[]. Let the index of the first occurrence be i.
2) Use Binary search to get index of the last occurrence of y in arr[]. Let the index of the last occurrence be j.
3) Return (j – i + 1);

For finding overlap:

Let two interval [x,y] and [p,q]

if p < x and q > y

return [x,y]

if p < x and q < y

return [x,q]

if p > x and p > y

No overlap

if p > x and p < y:

return [p,y]

// Please comment if this is what you are not looking for with details. I will update answer accordingly.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
CONFIDENCE INTERVAL PROJECT – Day 3 On this sheet, you will be calculating 95% Confidence Intervals...
CONFIDENCE INTERVAL PROJECT – Day 3 On this sheet, you will be calculating 95% Confidence Intervals for your data. Fill in all of the values for each piece, the formula, then show the full calculation for the CI. Write the CI in 2 different ways: In + notation In interval notation: ( ________ , ________ ) When you record standard deviation- round to the nearest tenth. Use the back of this sheet if extra space is needed. PROPORTION CONFIDENCE INTERVAL:...
How can I alter this Java method so that I convert and then return every sentence...
How can I alter this Java method so that I convert and then return every sentence in the array of strings instead of just the first sentence? public static char[] charConvert ( String [] sentences ) { int totLength = sentences[0].length() + sentences[1].length() + sentences[2].length() + sentences[3].length() + sentences[4].length(); char [] letter = new char[totLength]; int x = 0; int y = 0;    while ( y < sentences[x].length() ) { switch ( sentences[x].charAt(y) ) { case 'a': case 'A':...
Create a program using Binary Trees in Java to do the following 1. Verify a given...
Create a program using Binary Trees in Java to do the following 1. Verify a given expression is balanced in regards to parentheses 2. covert an infix expression to a postfix expression 3. Evaluate the expression Given expressions: String s[] = {"5 + ) * ( 2", " 2 + ( - 3 * 5 ) ", "(( 2 + 3 ) * 5 ) * 8 ", "5 * 10 + ( 15 - 20 ) ) - 25",...
5. Find the open intervals on which f(x) =x^3−6x^2−36x+ 2 is increasing, as well as the...
5. Find the open intervals on which f(x) =x^3−6x^2−36x+ 2 is increasing, as well as the open intervals on which f(x) is decreasing. - How do you know when the function is increasing or decreasing? Please show all work. 6. Find the open interval on which f(x) =x^3−6x^2−36x+ 2 has upward concavity, as well as the open intervals on which f(x) has downward con-cavity. - How do you know if a function has an upward concavity or downward concavity? Please...
I have a 6 sided die with the numbers 1, 2, 3, 3, 4, 5 on...
I have a 6 sided die with the numbers 1, 2, 3, 3, 4, 5 on it. I also have a hat filled with numbers. When I pick a number out of the hat, I get a number between 1 and 10. The hat number has a mean of 2 and a standard deviation of 2.5. I make a new random variable by combining these two previous random variables. The new random variable is made by taking the number I...
A)How do you convert 1/3 into binary with detailed steps ? B) Also how do you...
A)How do you convert 1/3 into binary with detailed steps ? B) Also how do you convert from repeating binary into decimal. (0.0110011001100110....0110) in base 2 . Assume 0110 patters go to forever. how can we convert it into decimal.
(Python 3) If I have a list of keys and 3 lists of values, how can...
(Python 3) If I have a list of keys and 3 lists of values, how can I append these values into an existing key, value pair in a dictionary? Say I have: mykeys = ["John", "Sarah", "Lexi, "Cass"] values1 = [3, 5, 2, 6] values2 = [17, 18, 12, 21] values3 = [4, 7, 3, 0] How can I make it so my dictionary contains all three values in each of the keys. i.e.: {"John" : [3, 17, 4], "Sarah":...
Java -How do I check if a number is valid without looping or any iterative processes?...
Java -How do I check if a number is valid without looping or any iterative processes? So I have a program that prompts the user to enter a number 1-5. (can include 1 and 5). It is to keep prompting the user until they enter a valid number. It's easy to do this with a while loop but for this assignment we are not supposed to use any type of loops or iterative processes. I am not good at recursion...
Hello I have a question about Java. example So when you put “sum 1 2 3”...
Hello I have a question about Java. example So when you put “sum 1 2 3” in console, you do not seperate those. Need a space between sum 1, 2 in the console. What is Sum? sum 1 2 3 6 (answer) sum 123456 21 sum 7 4 2 13 public static void main (String[] args) { System.out.Println(“What is Sum?”); String a=“”; a = scnr.nextLine(); String[] b = a.split(“”); if(b[0].equals(“sum”) { } I don’t know how to make code for...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT