Find least non | Java program to find the least non - overlapping number from a given set intervals ; Function to find the smallest non - overlapping number ; Sort the intervals based on their starting value ; Check if any missing value exist ; Finally print the missing value ; Driver code
import java . util . * ; import java . io . * ; class GFG { static class Pair implements Comparable < Pair > { int start , end ; Pair ( int s , int e ) { start = s ; end = e ; } public int compareTo ( Pair p ) { return this . start - p . start ; } } static void findMissing ( ArrayList < Pair > interval ) { Collections . sort ( interval ) ; int mx = 0 ; for ( int i = 0 ; i < interval . size ( ) ; ++ i ) { if ( interval . get ( i ) . start > mx ) { System . out . println ( mx ) ; return ; } else mx = Math . max ( mx , interval . get ( i ) . end + 1 ) ; } System . out . println ( mx ) ; } public static void main ( String [ ] args ) { ArrayList < Pair > interval = new ArrayList < > ( ) ; interval . add ( new Pair ( 0 , 14 ) ) ; interval . add ( new Pair ( 86 , 108 ) ) ; interval . add ( new Pair ( 22 , 30 ) ) ; interval . add ( new Pair ( 5 , 17 ) ) ; findMissing ( interval ) ; } }