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...
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",...
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:...
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.
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...
(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":...
Can i get the answer to these questions in detail explaining how you go to the...
Can i get the answer to these questions in detail explaining how you go to the solution 1.Convert to/from Hex (0) 2.Octal Binary Unsigned/ 2’s complement 3.Understand how to shift and rotate. 4.Understand the relationship between shifting and multiplication/division 5.Be able to recognize and use the following gates: AND OR NOT NAND NOR XOR 6.Understand how a half adder works Given a logical statement ((A OR B) AND NOT(C OR A)) show the gates. 7.What is the difference between an...
In regards to intersections, how would I add the events A (2, 3, 4, 5) and...
In regards to intersections, how would I add the events A (2, 3, 4, 5) and B (2, 3, 6, 7)? I know that A *inverted u like an umbrella* B will be (2, 3) because A and B both have these numbers in common What, then, happens to the rest? like A (4, 5) and B (6, 7) ? Please express to me how to write this and also the difference between the *u* between two letters or the...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT