Minimum time required to fill given N slots
Given an integer N which denotes the number of slots, and an array arr[] consisting of K integers in the range [1, N] . 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: 2
Explanation:
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: 0
Minimum time required to fill given N slots using Level Order Traversal:
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 and mark them as visited.
- 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.
- If any of the non visited index becomes visited in the current process Increment the time by 1 .
- 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++
#include <bits/stdc++.h>
using namespace std;
void minTime(vector< int > arr, int N, int K)
{
queue< int > q;
vector< bool > vis(N + 1, false );
int time = 0;
for ( int i = 0; i < K; i++) {
q.push(arr[i]);
vis[arr[i]] = true ;
}
while (q.size() > 0) {
bool op = false ;
for ( int i = 0; i < q.size(); i++) {
int curr = q.front();
q.pop();
if (curr - 1 >= 1 &&
!vis[curr - 1]) {
op = true ;
vis[curr - 1] = true ;
q.push(curr - 1);
}
if (curr + 1 <= N &&
!vis[curr + 1]) {
op = true ;
vis[curr + 1] = true ;
q.push(curr + 1);
}
}
time +=op;
}
cout << ( time );
}
int main()
{
int N = 6;
vector< int > arr = { 2,6 };
int K = arr.size();
minTime(arr, N, K);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void minTime( int arr[],
int N, int K)
{
Queue<Integer> q = new LinkedList<>();
boolean vis[] = new boolean [N + 1 ];
int time = 0 ;
for ( int i = 0 ; i < K; i++) {
q.add(arr[i]);
vis[arr[i]] = true ;
}
while (q.size() > 0 ) {
for ( int i = 0 ; i < q.size(); i++) {
int curr = q.poll();
if (curr - 1 >= 1 &&
!vis[curr - 1 ]) {
vis[curr - 1 ] = true ;
q.add(curr - 1 );
}
if (curr + 1 <= N &&
!vis[curr + 1 ]) {
vis[curr + 1 ] = true ;
q.add(curr + 1 );
}
}
time++;
}
System.out.println(time - 1 );
}
public static void main(String[] args)
{
int N = 6 ;
int arr[] = { 2 , 6 };
int K = arr.length;
minTime(arr, N, K);
}
}
|
Python3
def minTime(arr, N, K):
q = []
vis = [ False ] * (N + 1 )
time = 0
for i in range (K):
q.append(arr[i])
vis[arr[i]] = True
while ( len (q) > 0 ):
for i in range ( len (q)):
curr = q[ 0 ]
q.pop( 0 )
if (curr - 1 > = 1 and vis[curr - 1 ] = = 0 ):
vis[curr - 1 ] = True
q.append(curr - 1 )
if (curr + 1 < = N and vis[curr + 1 ] = = 0 ):
vis[curr + 1 ] = True
q.append(curr + 1 )
time + = 1
print (time - 1 )
N = 6
arr = [ 2 , 6 ]
K = len (arr)
minTime(arr, N, K)
|
C#
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);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Minimum time required to fill given N slots using Sorting:
Another approach to solve this problem can be done by sorting the array of filled slots and then iterating through the array to find the maximum distance between adjacent filled slots. The minimum time required to fill all the slots would be equal to the maximum distance between adjacent filled slots.
- Declare a function minTime() which takes three arguments: vector of integers arr, integer N, and integer K.
- Sort the vector arr[] using the sort function from STL.
- Declare an integer variable maxDist and initialize it to zero.
- Traverse the sorted array arr and find the maximum distance between adjacent slots, store this distance in the maxDist variable.
- Check the distance from the first and last slot to the closest filled slot and store the maximum of these two distances in the maxDist variable.
- Subtract 1 from maxDist to account for the time it takes to fill each slot.
- Print the value of maxDist.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minTime(vector< int > arr, int N, int K)
{
sort(arr.begin(), arr.end());
int maxDist = 0;
for ( int i = 1; i < K; i++) {
maxDist = max(maxDist, arr[i] - arr[i - 1] - 1);
}
maxDist = (maxDist + 1) / 2;
maxDist = max(maxDist, arr[0] - 1);
maxDist = max(maxDist, N - arr[K - 1]);
cout << maxDist;
}
int main()
{
int N = 6;
vector< int > arr = { 2, 6 };
int K = arr.size();
minTime(arr, N, K);
}
|
Java
import java.util.*;
public class Main {
static void minTime(List<Integer> arr, int N, int K) {
Collections.sort(arr);
int maxDist = 0 ;
for ( int i = 1 ; i < K; i++) {
maxDist = Math.max(maxDist, arr.get(i) - arr.get(i - 1 ) - 1 );
}
maxDist = Math.max(maxDist, arr.get( 0 ) - 1 );
maxDist = Math.max(maxDist, N - arr.get(K - 1 ));
maxDist--;
System.out.println(maxDist);
}
public static void main(String[] args) {
int N = 6 ;
List<Integer> arr = new ArrayList<Integer>();
arr.add( 2 );
arr.add( 6 );
int K = arr.size();
minTime(arr, N, K);
}
}
|
Python
def minTime(arr, N, K):
arr.sort()
maxDist = 0
for i in range ( 1 , K):
maxDist = max (maxDist, arr[i] - arr[i - 1 ] - 1 )
maxDist = max (maxDist, arr[ 0 ] - 1 )
maxDist = max (maxDist, N - arr[K - 1 ])
maxDist - = 1
print (maxDist)
if __name__ = = "__main__" :
N = 6
arr = [ 2 , 6 ]
K = len (arr)
minTime(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void MinTime(List< int > arr, int N, int K)
{
arr.Sort();
int maxDist = 0;
for ( int i = 1; i < K; i++)
{
maxDist = Math.Max(maxDist, arr[i] - arr[i - 1] - 1);
}
maxDist = Math.Max(maxDist, arr[0] - 1);
maxDist = Math.Max(maxDist, N - arr[K - 1]);
maxDist--;
Console.WriteLine(maxDist);
}
public static void Main()
{
int N = 6;
List< int > arr = new List< int > { 2, 6 };
int K = arr.Count;
MinTime(arr, N, K);
}
}
|
Javascript
function minTime(arr, N, K) {
arr.sort((a, b) => a - b);
let maxDist = 0;
for (let i = 1; i < K; i++) {
maxDist = Math.max(maxDist, arr[i] - arr[i - 1] - 1);
}
maxDist = Math.max(maxDist, arr[0] - 1);
maxDist = Math.max(maxDist, N - arr[K - 1]);
maxDist--;
console.log(maxDist);
}
const N = 6;
const arr = [2, 6];
const K = arr.length;
minTime(arr, N, K);
|
Output:-
2
Time Complexity: O(KlogK), then it loops through the vector once to calculate the maximum distance between adjacent slots and twice to calculate the distance from the first and last slot to the closest filled slot, which takes O(K) time. Therefore, the overall time complexity is O(KlogK).
Auxiliary Space: O(1).
Last Updated :
10 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...