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
def minTime ( arr , N , K ) : NEW_LINE INDENT q = [ ] NEW_LINE vis = [ False ] * ( N + 1 ) NEW_LINE time = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT q . append ( arr [ i ] ) NEW_LINE vis [ arr [ i ] ] = True NEW_LINE DEDENT while ( len ( q ) > 0 ) : NEW_LINE INDENT for i in range ( len ( q ) ) : NEW_LINE INDENT curr = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( curr - 1 >= 1 and vis [ curr - 1 ] == 0 ) : NEW_LINE INDENT vis [ curr - 1 ] = True NEW_LINE q . append ( curr - 1 ) NEW_LINE DEDENT if ( curr + 1 <= N and vis [ curr + 1 ] == 0 ) : NEW_LINE INDENT vis [ curr + 1 ] = True NEW_LINE q . append ( curr + 1 ) NEW_LINE DEDENT DEDENT time += 1 NEW_LINE DEDENT print ( time - 1 ) NEW_LINE DEDENT N = 6 NEW_LINE arr = [ 2 , 6 ] NEW_LINE K = len ( arr ) NEW_LINE minTime ( arr , N , K ) NEW_LINE