Minimum time required to fill given N slots | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static void minTime ( List < int > arr , int N , int K ) { Queue < int > q = new Queue < int > ( ) ; int [ ] vis = new int [ N + 1 ] ; Array . Clear ( vis , 0 , vis . Length ) ; int time = 0 ; for ( int i = 0 ; i < K ; i ++ ) { q . Enqueue ( arr [ i ] ) ; vis [ arr [ i ] ] = 1 ; } while ( q . Count > 0 ) { for ( int i = 0 ; i < q . Count ; i ++ ) { int curr = q . Peek ( ) ; q . Dequeue ( ) ; if ( curr - 1 >= 1 && vis [ curr - 1 ] == 0 ) { vis [ curr - 1 ] = 1 ; q . Enqueue ( curr - 1 ) ; } if ( curr + 1 <= N && vis [ curr + 1 ] == 0 ) { vis [ curr + 1 ] = 1 ; q . Enqueue ( curr + 1 ) ; } } time ++ ; } Console . WriteLine ( time - 1 ) ; } public static void Main ( ) { int N = 6 ; List < int > arr = new List < int > ( ) { 2 , 6 } ; int K = arr . Count ; minTime ( arr , N , K ) ; } }