Count intervals that intersects with a given meeting time | Java implementation of the above approach ; Function to convert a time in 24 hour format to an equivalent integer ; Removes " : " at 3 rd position ; Calculate hours ; Stores the time in 24 hours format ; If time is in " AM " ; If hh is equal to 12 ; If time is in " PM " ; If hh is equal to 12 ; Return time ; Function to count number of intervals in which p lies ; Stores the count ; Stores the integer value of 24 hours time format of P ; Traverse the array ; Stores the integer value of 24 hours time format of arr [ i ] [ 0 ] ; Stores the integer value of 24 hours time format of arr [ i ] [ 1 ] ; If M lies within the [ L , R ] ; Increment ans by 1 ; Return ans ; Driver Code
import java . io . * ; class GFG { static int convert ( String str ) { str = str . substring ( 0 , 2 ) + str . substring ( 3 ) ; int h1 = ( int ) str . charAt ( 1 ) - '0' ; int h2 = ( int ) str . charAt ( 0 ) - '0' ; int hh = ( h2 * 10 + h1 % 10 ) ; int time = 0 ; if ( str . charAt ( 5 ) == ' A ' ) { if ( hh == 12 ) time += Integer . parseInt ( str . substring ( 2 , 4 ) ) ; else { time += Integer . parseInt ( str . substring ( 0 , 2 ) ) ; } } else { if ( hh == 12 ) { time += Integer . parseInt ( str . substring ( 0 , 4 ) ) ; } else { time += Integer . parseInt ( str . substring ( 0 , 4 ) ) ; time += 1200 ; } } return time ; } static int countOverlap ( String arr [ ] [ ] , int n , String p ) { int ans = 0 ; int M = convert ( p ) ; for ( int i = 0 ; i < n ; i ++ ) { int L = convert ( arr [ i ] [ 0 ] ) ; int R = convert ( arr [ i ] [ 1 ] ) ; if ( ( L <= M && M <= R ) || ( M >= R && M <= L ) ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { String [ ] [ ] arr = new String [ ] [ ] { { "12:00 : AM " , "11:55 : PM " } , { "12:01 : AM " , "11:50 : AM " } , { "12:30 : AM " , "12:00 : PM " } , { "11:57 : AM " , "11:59 : PM " } } ; String P = "12:01 : PM " ; int N = arr . length ; System . out . println ( countOverlap ( arr , N , P ) ) ; } }