Minimum time required to fill given N slots | Function to return the minimum time to fill all the slots ; Stores visited slots ; Checks if a slot is visited or not ; Insert all filled slots ; Iterate until queue is not empty ; Iterate through all slots in the queue ; Front index ; If previous slot is present and not visited ; If next slot is present and not visited ; Increment the time at each level ; Print the answer ; Driver Code ; Function Call
function minTime ( arr , N , K ) { var q = [ ] ; var vis = Array ( N + 1 ) . fill ( false ) ; var time = 0 ; for ( var i = 0 ; i < K ; i ++ ) { q . push ( arr [ i ] ) ; vis [ arr [ i ] ] = true ; } while ( q . length > 0 ) { for ( var i = 0 ; i < q . length ; i ++ ) { var curr = q [ 0 ] ; q . pop ( ) ; if ( curr - 1 >= 1 && ! vis [ curr - 1 ] ) { vis [ curr - 1 ] = true ; q . push ( curr - 1 ) ; } if ( curr + 1 <= N && ! vis [ curr + 1 ] ) { vis [ curr + 1 ] = true ; q . push ( curr + 1 ) ; } } time ++ ; } document . write ( time - 1 ) ; } var N = 6 ; var arr = [ 2 , 6 ] ; var K = arr . length ; minTime ( arr , N , K ) ;