Question

In: Computer Science

Check if any two intervals overlap among a given set of intervals, return true if they overlap and false if they don't.

USE JAVA

Check if any two intervals overlap among a given set of intervals, return true if they overlap and false if they don't.

Input:  arr[] = {{1, 3}, {5, 7}, {2, 4}, {6, 8}}
Output: true
The intervals {1, 3} and {2, 4} overlap

Solutions

Expert Solution

import java.util.*;
import java.io.*;
import javafx.util.*;

public class MyClass {
public static boolean overlap(ArrayList> list){
if(list.size() < 1)
return false;
list.sort((i1, i2) -> Integer.compare(i1.getKey(), i2.getKey()));
for(int i = 1; i < list.size(); i++) {
if(list.get(i).getKey() <= list.get(i-1).getValue()) {
return true;
}
}
return false;
}
  
public static void main(String args[]) {
ArrayList> list = new ArrayList> ();
list.add(new Pair (1,3));
list.add(new Pair (5,7));
list.add(new Pair (2,4));
list.add(new Pair (6,8));
System.out.println(overlap(list));
}
}


Related Solutions

TRUE OR FALSE? ** IF YOU CAN'T ANSWER THEM ALL, PLEASE DON'T ANSWER ANY** When a...
TRUE OR FALSE? ** IF YOU CAN'T ANSWER THEM ALL, PLEASE DON'T ANSWER ANY** When a company declares of cash dividends retained earnings is reduced. Authorized stock is the total number of shares outstanding. The asset turnover ratio measures how efficiently a company uses its assets to generate sales. Pro forma income usually excludes items that the company thinks are unusual or nonrecurring. Sales minus operating expenses equals gross profit. Freight terms of FOB Destination means that the seller pays...
1. True or False: Confidence intervals can only be calculated for means. True False SAVE 2....
1. True or False: Confidence intervals can only be calculated for means. True False SAVE 2. Suppose a sample of 200 barrow-wights (wraith-like creatures that feed on the life force of the living) yields a sample mean of 6.2 living beings consumed per day. Assuming a standard error of 1.7, what would the upper bound of a 95% confidence interval be? SAVE 3. Using the same ridiculous survey data from the previous question, what would the lower bound of a...
classify as true or false. a) any two rectangles are similar. b) if an angle of...
classify as true or false. a) any two rectangles are similar. b) if an angle of one rhombus is congruent to an angle of a second rhombus, then the two rhombi are similar.
1. True or False: Confidence intervals can only be calculated for means
1. True or False: Confidence intervals can only be calculated for means
True or False: In any Nash equilibrium, if a player is paying two (or more) strategies...
True or False: In any Nash equilibrium, if a player is paying two (or more) strategies with positive probability, then she is indifferent between any of those strategies. (provide examples)
True or False? For any two events A and B, P(A∩ B) ≥ 1 − P(A∪...
True or False? For any two events A and B, P(A∩ B) ≥ 1 − P(A∪ B) True or False? For any two independent events A and B, P(A| B) = P(A| Bc ) Compute B(6, .2, 2)
a) TRUE OR FALSE: For a set of data with a mean of 18 and a...
a) TRUE OR FALSE: For a set of data with a mean of 18 and a variance of 25, approximately 68% of the values will fall between 13 to 23. b) Which of the following statement is true about the relationship between a sample and a population 1)Every sample is a perfect representation of a population 2)The sample size is smaller than a population size 3)Every member of a population is also in the sample 4)A population size is smaller...
Answer each question by True or False. Justify your answer. (1) True or False? The set...
Answer each question by True or False. Justify your answer. (1) True or False? The set V = {p ∈ P2: p (7) = 0, p’ (7) = 0} is a subspace of P2. (2) True or False? The set of 2 by 2 matrices whose entries are either all 0 or all nonzero is a subspace of the set of all 2 by 2 matrices M2×2(R). (3) True or False? The set of all functions in C([0, 1]) such...
True or False (T/F) a. When the correlation coefficient of the two asset’s return is 1,...
True or False (T/F) a. When the correlation coefficient of the two asset’s return is 1, there is the greatest diversification benefit. ( ) b. If the NPV of a project is negative, the project should be accepted ( ). c. Increase in net working capital indicates cash inflow for a project ( ).
True or False. 1. If the set {v1,v2} is a basis of R^2, then the set...
True or False. 1. If the set {v1,v2} is a basis of R^2, then the set {v1,v1+v2} is also a basis of R^2. 2.If W be a vector space and V1,V2 are subspaces of W, then V1 u V2 is also a subspace of W. V1 u V2 denotes the union of V1 and V2, i.e. the set of vectors which belong to either V1 or V2 (or to both). 3.If W be a vector space and V1,V2 are subspaces...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT