Maximum number of intervals that an interval can intersect | Java program for the above approach ; Pair class to store in the um as a key with hashcode and equals ; Function to count maximum number of intervals that an interval can intersect ; Create a hashmap ; Sort by starting position ; Traverse all the intervals ; Initialize um for v [ i ] as 0 ; Store the starting and ending point of the ith interval ; Set low and high ; Store the required number of intervals ; Apply binary search to find the number of intervals completely lying to the right of the ith interval ; Find the mid ; Condition for searching in the first half ; Otherwise search in the second half ; Increment um for v [ i ] by n - ans if ans != - 1 ; Sort by ending position ; Traverse all the intervals ; Store the starting and ending point of the ith interval ; Set low and high ; Store the required number of intervals ; Apply binary search to find the number of intervals completely lying to the left of the ith interval ; Increment um for v [ i ] by ans + 1 if ans != - 1 ; Store the answer ; Traverse the map ; Update the overall answer ; Print the answer ; Driver Code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Pair { int first ; int second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } @ Override public int hashCode ( ) { final int prime = 31 ; int result = 1 ; result = prime * result + first ; result = prime * result + second ; return result ; } @ Override public boolean equals ( Object obj ) { if ( this == obj ) return true ; if ( obj == null ) return false ; if ( getClass ( ) != obj . getClass ( ) ) return false ; Pair other = ( Pair ) obj ; if ( first != other . first ) return false ; if ( second != other . second ) return false ; return true ; } } static void findMaxIntervals ( ArrayList < Pair > v , int n ) { HashMap < Pair , Integer > um = new HashMap < > ( ) ; Collections . sort ( v , ( x , y ) -> x . first - y . first ) ; for ( int i = 0 ; i < n ; i ++ ) { um . put ( v . get ( i ) , 0 ) ; int start = v . get ( i ) . first ; int end = v . get ( i ) . second ; int low = i + 1 ; int high = n - 1 ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( v . get ( mid ) . first > end ) { ans = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( ans != - 1 ) um . put ( v . get ( i ) , n - ans ) ; } Collections . sort ( v , ( x , y ) -> x . second - y . second ) ; for ( int i = 0 ; i < n ; i ++ ) { int start = v . get ( i ) . first ; int end = v . get ( i ) . second ; int low = 0 ; int high = i - 1 ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( v . get ( mid ) . second < start ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } if ( ans != - 1 ) um . put ( v . get ( i ) , um . get ( v . get ( i ) ) + ( ans + 1 ) ) ; } int res = 0 ; for ( int second : um . values ( ) ) { res = Math . max ( res , n - second ) ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { ArrayList < Pair > arr = new ArrayList < > ( ) ; arr . add ( new Pair ( 1 , 2 ) ) ; arr . add ( new Pair ( 3 , 4 ) ) ; arr . add ( new Pair ( 2 , 5 ) ) ; int N = arr . size ( ) ; findMaxIntervals ( arr , N ) ; } }