Given an integer **N** which denotes the number of slots, and an array **arr[]** consisting of **K** integers in the range** [1, N] **repreand. Each element of the array are in the range **[1, N]** which represents the indices of the filled slots. At each unit of time, the index with filled slot fills the adjacent empty slots. The task is to find the minimum time taken to fill all the **N** slots.

**Examples:**

Input:N = 6, K = 2, arr[] = {2, 6}Output:2Explanation:

Initially there are 6 slots and the indices of the filled slots are slots[] = {0, 2, 0, 0, 0, 6}, where 0 represents unfilled.

After 1 unit of time, slots[] = {1, 2, 3, 0, 5, 6}

After 2 units of time, slots[] = {1, 2, 3, 4, 5, 6}

Therefore, the minimum time required is 2.

Input:N = 5, K = 5, arr[] = {1, 2, 3, 4, 5}Output:1

**Approach:** To solve the given problem, the idea is to perform Level Order Traversal** **on the given **N** slots using a Queue. Follow the steps below to solve the problem:

- Initialize a variable, say
**time**as**0**, and an auxiliary array**visited[]**to mark the filled indices in each iteration. - Now, push the indices of filled slots given in array
**arr[]**in a queue - Now, iterate until the queue is not empty and perform the following steps:
- Remove the front index
**i**from the queue and if the adjacent slots**(i – 1)**and**(i + 1)**are in the range**[1, N]**and are unvisited, then mark them as visited and push them into the queue. - Increment the
**time**by**1**.

- Remove the front index
- After completing the above steps, print the value of
**(time – 1)**as the minimum time required to fill all the slots.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to return the minimum` `// time to fill all the slots` `void` `minTime(vector<` `int` `> arr, ` `int` `N, ` `int` `K)` `{` ` ` ` ` `// Stores visited slots` ` ` `queue<` `int` `> q;` ` ` ` ` `// Checks if a slot is visited or not` ` ` `vector<` `bool` `> vis(N + 1, ` `false` `);` ` ` `int` `time` `= 0;` ` ` `// Insert all filled slots` ` ` `for` `(` `int` `i = 0; i < K; i++) {` ` ` `q.push(arr[i]);` ` ` `vis[arr[i]] = ` `true` `;` ` ` `}` ` ` `// Iterate until queue is` ` ` `// not empty` ` ` `while` `(q.size() > 0) {` ` ` `// Iterate through all slots` ` ` `// in the queue` ` ` `for` `(` `int` `i = 0; i < q.size(); i++) {` ` ` `// Front index` ` ` `int` `curr = q.front();` ` ` `q.pop();` ` ` `// If previous slot is` ` ` `// present and not visited` ` ` `if` `(curr - 1 >= 1 &&` ` ` `!vis[curr - 1]) {` ` ` `vis[curr - 1] = ` `true` `;` ` ` `q.push(curr - 1);` ` ` `}` ` ` `// If next slot is present` ` ` `// and not visited` ` ` `if` `(curr + 1 <= N &&` ` ` `!vis[curr + 1]) {` ` ` `vis[curr + 1] = ` `true` `;` ` ` `q.push(curr + 1);` ` ` `}` ` ` `}` ` ` `// Increment the time` ` ` `// at each level` ` ` `time` `++;` ` ` `}` ` ` `// Print the answer` ` ` `cout << (` `time` `- 1);` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 6;` ` ` `vector<` `int` `> arr = { 2, 6 };` ` ` `int` `K = arr.size();` ` ` `// Function Call` ` ` `minTime(arr, N, K);` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG {` ` ` `// Function to return the minimum` ` ` `// time to fill all the slots` ` ` `public` `static` `void` `minTime(` `int` `arr[],` ` ` `int` `N, ` `int` `K)` ` ` `{` ` ` ` ` `// Stores visited slots` ` ` `Queue<Integer> q = ` `new` `LinkedList<>();` ` ` `// Checks if a slot is visited or not` ` ` `boolean` `vis[] = ` `new` `boolean` `[N + ` `1` `];` ` ` `int` `time = ` `0` `;` ` ` `// Insert all filled slots` ` ` `for` `(` `int` `i = ` `0` `; i < K; i++) {` ` ` `q.add(arr[i]);` ` ` `vis[arr[i]] = ` `true` `;` ` ` `}` ` ` `// Iterate until queue is` ` ` `// not empty` ` ` `while` `(q.size() > ` `0` `) {` ` ` `// Iterate through all slots` ` ` `// in the queue` ` ` `for` `(` `int` `i = ` `0` `; i < q.size(); i++) {` ` ` `// Front index` ` ` `int` `curr = q.poll();` ` ` `// If previous slot is` ` ` `// present and not visited` ` ` `if` `(curr - ` `1` `>= ` `1` `&&` ` ` `!vis[curr - ` `1` `]) {` ` ` `vis[curr - ` `1` `] = ` `true` `;` ` ` `q.add(curr - ` `1` `);` ` ` `}` ` ` `// If next slot is present` ` ` `// and not visited` ` ` `if` `(curr + ` `1` `<= N &&` ` ` `!vis[curr + ` `1` `]) {` ` ` `vis[curr + ` `1` `] = ` `true` `;` ` ` `q.add(curr + ` `1` `);` ` ` `}` ` ` `}` ` ` `// Increment the time` ` ` `// at each level` ` ` `time++;` ` ` `}` ` ` `// Print the answer` ` ` `System.out.println(time - ` `1` `);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `6` `;` ` ` `int` `arr[] = { ` `2` `, ` `6` `};` ` ` `int` `K = arr.length;` ` ` `// Function Call` ` ` `minTime(arr, N, K);` ` ` `}` `}` |

## Python3

`# Python3 program for the above approach` `# Function to return the minimum` `# time to fill all the slots` `def` `minTime(arr, N, K):` ` ` ` ` `# Stores visited slots` ` ` `q ` `=` `[]` ` ` ` ` `# Checks if a slot is visited or not` ` ` `vis ` `=` `[` `False` `] ` `*` `(N ` `+` `1` `)` ` ` `time ` `=` `0` ` ` `# Insert all filled slots` ` ` `for` `i ` `in` `range` `(K):` ` ` `q.append(arr[i])` ` ` `vis[arr[i]] ` `=` `True` ` ` ` ` `# Iterate until queue is` ` ` `# not empty` ` ` `while` `(` `len` `(q) > ` `0` `):` ` ` ` ` `# Iterate through all slots` ` ` `# in the queue` ` ` `for` `i ` `in` `range` `(` `len` `(q)):` ` ` ` ` `# Front index` ` ` `curr ` `=` `q[` `0` `]` ` ` `q.pop(` `0` `)` ` ` `# If previous slot is` ` ` `# present and not visited` ` ` `if` `(curr ` `-` `1` `>` `=` `1` `and` `vis[curr ` `-` `1` `] ` `=` `=` `0` `):` ` ` `vis[curr ` `-` `1` `] ` `=` `True` ` ` `q.append(curr ` `-` `1` `)` ` ` ` ` `# If next slot is present` ` ` `# and not visited` ` ` `if` `(curr ` `+` `1` `<` `=` `N ` `and` `vis[curr ` `+` `1` `] ` `=` `=` `0` `):` ` ` `vis[curr ` `+` `1` `] ` `=` `True` ` ` `q.append(curr ` `+` `1` `)` ` ` ` ` `# Increment the time` ` ` `# at each level` ` ` `time ` `+` `=` `1` ` ` ` ` `# Print the answer` ` ` `print` `(time ` `-` `1` `)` `# Driver Code` `N ` `=` `6` `arr ` `=` `[ ` `2` `, ` `6` `]` `K ` `=` `len` `(arr)` `# Function Call` `minTime(arr, N, K)` `# This code is contributed by susmitakundugoaldanga` |

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG` `{` ` ` `// Function to return the minimum` `// time to fill all the slots` `static` `void` `minTime(List<` `int` `> arr, ` `int` `N, ` `int` `K)` `{` ` ` ` ` `// Stores visited slots` ` ` `Queue<` `int` `> q = ` `new` `Queue<` `int` `>();` ` ` ` ` `// Checks if a slot is visited or not` ` ` `int` `[]vis = ` `new` `int` `[N + 1];` ` ` `Array.Clear(vis, 0, vis.Length);` ` ` `int` `time = 0;` ` ` `// Insert all filled slots` ` ` `for` `(` `int` `i = 0; i < K; i++)` ` ` `{` ` ` `q.Enqueue(arr[i]);` ` ` `vis[arr[i]] = 1;` ` ` `}` ` ` `// Iterate until queue is` ` ` `// not empty` ` ` `while` `(q.Count > 0)` ` ` `{` ` ` `// Iterate through all slots` ` ` `// in the queue` ` ` `for` `(` `int` `i = 0; i < q.Count; i++)` ` ` `{` ` ` `// Front index` ` ` `int` `curr = q.Peek();` ` ` `q.Dequeue();` ` ` `// If previous slot is` ` ` `// present and not visited` ` ` `if` `(curr - 1 >= 1 &&` ` ` `vis[curr - 1]==0)` ` ` `{` ` ` `vis[curr - 1] = 1;` ` ` `q.Enqueue(curr - 1);` ` ` `}` ` ` `// If next slot is present` ` ` `// and not visited` ` ` `if` `(curr + 1 <= N &&` ` ` `vis[curr + 1] == 0)` ` ` `{` ` ` `vis[curr + 1] = 1;` ` ` `q.Enqueue(curr + 1);` ` ` `}` ` ` `}` ` ` `// Increment the time` ` ` `// at each level` ` ` `time++;` ` ` `}` ` ` `// Print the answer` ` ` `Console.WriteLine(time-1);` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `int` `N = 6;` ` ` `List<` `int` `> arr = ` `new` `List<` `int` `>() { 2, 6 };` ` ` `int` `K = arr.Count;` ` ` `// Function Call` ` ` `minTime(arr, N, K);` `}` `}` `// THIS CODE IS CONTRIBUTED BY SURENDRA_GANGWAR.` |

**Output:**

2

**Time Complexity:** O(N)**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.