text: Maximum time such that absolute difference between hour and minute lies in given range | Function checks whether given time is correct ; To check minute value of time ; To check hour value of time ; Changes in value is not allowed at position where ' ? ' is not present ; Function checks whether the absolute difference between hour and minute value is within [ L , R ] ; Checks if the difference is outside the give range ; Displays time in proper 24 - hour format ; Function find the desired value of time whose difference lies in the range [ L , R ] ; Decrease hour value from 23 to 0 ; Check if the hour value is valid if not valid then no need to change minute value , since time will still remain in valid , to check hour value flag is set to 1. ; Decrease minute value from 59 to 0 ; Check if the minute value is valid , if not valid then skip the current iteration , to check ' minute ' value flag is set to 0. ; Input time ; Difference range
code: function isValid ( a1 , a2 , str , flag ) { let v1 , v2 ; if ( flag == 0 ) { v1 = str [ 4 ] ; v2 = str [ 3 ] ; } else { v1 = str [ 1 ] ; v2 = str [ 0 ] ; } if ( v1 != a1 && v1 != ' ' ) return false ; if ( v2 != a2 && v2 != ' ' ) return false ; return true ; } function inRange ( hh , mm , L , R ) { let a = Math . abs ( hh - mm ) ; if ( a < L a > R ) return false ; return true ; } function displayTime ( hh , mm ) { if ( hh > 10 ) document . write ( hh + " " ) ; else if ( hh < 10 ) document . write ( " " + hh + " " ) ; if ( mm > 10 ) document . write ( mm + " " ) ; else if ( mm < 10 ) document . write ( " " + mm + " " ) ; } function maximumTimeWithDifferenceInRange ( str , L , R ) { let i = 0 , j = 0 ; let h1 , h2 , m1 , m2 ; for ( i = 23 ; i >= 0 ; i -- ) { h1 = i % 10 ; h2 = Math . floor ( i / 10 ) ; if ( ! isValid ( String . fromCharCode ( h1 ) , String . fromCharCode ( h2 ) , str , 1 ) ) { continue ; } for ( j = 59 ; j >= 0 ; j -- ) { m1 = j % 10 ; m2 = Math . floor ( j / 10 ) ; if ( ! isValid ( String . fromCharCode ( m1 ) , String . fromCharCode ( m2 ) , str , 0 ) ) { continue ; } if ( inRange ( i , j , L , R ) ) { displayTime ( i , j ) ; return ; } } } if ( inRange ( i , j , L , R ) ) displayTime ( i , j ) ; else document . write ( " " ) ; } let timeValue = " " ; let L = 20 , R = 39 ; maximumTimeWithDifferenceInRange ( timeValue , L , R ) ;