Minimum time to complete at least K tasks when everyone rest after each task
Last Updated :
04 Mar, 2025
Given an array arr[] of size n representing the time taken by a person to complete a task. Also, an array restTime[] which denotes the amount of time one person takes to rest after finishing a task. Each person is independent of others i.e. they can work simultaneously on different tasks at the same time. Given an integer k, the task is to find at least how much time will be taken to complete all the k tasks by all the persons.
Examples:
Input: arr[] = [1, 2, 4], restTime[] = [1, 2, 2], K = 5
Output: 5
Explanation: At t = 1, tasks task done = [1, 0, 0]Total task = 1
At t = 2, No of tasks completed = [1, 1, 0],
Total task = 1 + 1 = 2. Because 1st person was taking rest
At t = 3, No of tasks completed = [2, 1, 0],
Total task = 2 + 1 = 3, Because 2nd person was taking rest
At t = 4, No of tasks completed = [2, 1, 1],
Total task = 2 + 1 + 1 = 4, Because 1st and 2nd person was taking rest
At t = 5, No of tasks completed = [3, 1, 1].
Total task = 3 + 1 + 1 = 5. Minimum time taken = 5.
Input: arr[] = [1, 2, 4, 7, 8], restTime[] = [4, 1, 2, 3, 1], k = 2
Output: 2
Approach - Using Binary Search
The idea is to use binary search as we observe the problem, we note that if a given time mid is sufficient, then all greater times will also be sufficient, making it suitable for binary search. We set the search space between min(arr) * k and max(arr) * k and use binary search to find the smallest valid time. For each mid value, we check if k tasks can be completed by counting tasks each worker can do within mid time. If mid is valid, we reduce the time range; otherwise, we increase it, ensuring an optimal solution efficiently.
C++
// C++ program to find the minimum time required
// to complete k tasks using Binary Search
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible to complete
// at least k tasks within the given time mid
bool timePossible(int mid, int k, vector<int>& arr,
vector<int>& restTime) {
int curTask = 0;
// Loop through each worker
for (int i = 0; i < arr.size(); i++) {
int totalTime = arr[i] + restTime[i];
curTask += mid / totalTime;
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i])
curTask++;
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
int minimumTimeTaken(vector<int>& arr,
vector<int>& restTime, int k) {
// Lower bound of time
int st = arr[0] * k;
// Upper bound of time
int end = arr.back() * k;
while (st <= end) {
int mid = st + (end - st) / 2;
// If mid time is possible, try minimizing it
if (timePossible(mid, k, arr, restTime))
end = mid - 1;
else
st = mid + 1;
}
// Minimum time required
return st;
}
// Driver code
int main() {
vector<int> arr = { 1, 2, 4 };
vector<int> restTime = { 1, 2, 2 };
int k = 5;
cout << minimumTimeTaken(arr, restTime, k);
return 0;
}
Java
// Java program to find the minimum time required
// to complete k tasks using Binary Search
import java.util.*;
class GfG {
// Function to check if it is possible to complete
// at least k tasks within the given time mid
static boolean timePossible(int mid, int k,
int[] arr, int[] restTime) {
int curTask = 0;
// Loop through each worker
for (int i = 0; i < arr.length; i++) {
int totalTime = arr[i] + restTime[i];
curTask += mid / totalTime;
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i])
curTask++;
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
static int minimumTimeTaken(int[] arr,
int[] restTime, int k) {
// Lower bound of time
int st = arr[0] * k;
// Upper bound of time
int end = arr[arr.length - 1] * k;
while (st <= end) {
int mid = st + (end - st) / 2;
// If mid time is possible, try minimizing it
if (timePossible(mid, k, arr, restTime))
end = mid - 1;
else
st = mid + 1;
}
// Minimum time required
return st;
}
// Driver code
public static void main(String[] args) {
int[] arr = { 1, 2, 4 };
int[] restTime = { 1, 2, 2 };
int k = 5;
System.out.println(minimumTimeTaken(arr, restTime, k));
}
}
Python
# Python program to find the minimum time required
# to complete k tasks using Binary Search
def timePossible(mid, k, arr, restTime):
curTask = 0
# Loop through each worker
for i in range(len(arr)):
totalTime = arr[i] + restTime[i]
curTask += mid // totalTime
# Check if remaining time allows an extra task
if mid % totalTime >= arr[i]:
curTask += 1
# If required tasks are completed, return True
if curTask >= k:
return True
return False
# Function to find the minimum time required
# to complete k tasks
def minimumTimeTaken(arr, restTime, k):
# Lower bound of time
st = arr[0] * k
# Upper bound of time
end = arr[-1] * k
while st <= end:
mid = st + (end - st) // 2
# If mid time is possible, try minimizing it
if timePossible(mid, k, arr, restTime):
end = mid - 1
else:
st = mid + 1
# Minimum time required
return st
# Driver code
if __name__ == "__main__":
arr = [1, 2, 4]
restTime = [1, 2, 2]
k = 5
print(minimumTimeTaken(arr, restTime, k))
C#
// C# program to find the minimum time required
// to complete k tasks using Binary Search
using System;
class GfG {
// Function to check if it is possible to complete
// at least k tasks within the given time mid
static bool TimePossible(int mid, int k,
int[] arr, int[] restTime) {
int curTask = 0;
// Loop through each worker
for (int i = 0; i < arr.Length; i++) {
int totalTime = arr[i] + restTime[i];
curTask += mid / totalTime;
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i])
curTask++;
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
static int MinimumTimeTaken(int[] arr,
int[] restTime, int k) {
// Lower bound of time
int st = arr[0] * k;
// Upper bound of time
int end = arr[arr.Length - 1] * k;
while (st <= end) {
int mid = st + (end - st) / 2;
// If mid time is possible, try minimizing it
if (TimePossible(mid, k, arr, restTime))
end = mid - 1;
else
st = mid + 1;
}
// Minimum time required
return st;
}
// Driver code
public static void Main() {
int[] arr = { 1, 2, 4 };
int[] restTime = { 1, 2, 2 };
int k = 5;
Console.WriteLine(MinimumTimeTaken(arr, restTime, k));
}
}
JavaScript
// JavaScript program to find the minimum time required
// to complete k tasks using Binary Search
function timePossible(mid, k, arr, restTime) {
let curTask = 0;
// Loop through each worker
for (let i = 0; i < arr.length; i++) {
let totalTime = arr[i] + restTime[i];
curTask += Math.floor(mid / totalTime);
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i]) {
curTask++;
}
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
function minimumTimeTaken(arr, restTime, k) {
// Lower bound of time
let st = arr[0] * k;
// Upper bound of time
let end = arr[arr.length - 1] * k;
while (st <= end) {
let mid = st + Math.floor((end - st) / 2);
// If mid time is possible, try minimizing it
if (timePossible(mid, k, arr, restTime)) {
end = mid - 1;
} else {
st = mid + 1;
}
}
// Minimum time required
return st;
}
// Driver code
let arr = [1, 2, 4];
let restTime = [1, 2, 2];
let k = 5;
console.log(minimumTimeTaken(arr, restTime, k));
Time Complexity: O(n log (k * max(arr))), where n is the number of workers, and k * max(arr) is the search space.
Auxiliary Space: O(1), as only a few extra variables are used.
Similar Reads
Binary Search on Answer Tutorial with Problems Binary Search on Answer is the algorithm in which we are finding our answer with the help of some particular conditions. We have given a search space in which we take an element [mid] and check its validity as our answer, if it satisfies our given condition in the problem then we store its value and
15+ min read
Time Crunch Challenge Geeks for Geeks is organizing a hackathon consisting of N sections, each containing K questions. The duration of the hackathon is H hours. Each participant can determine their speed of solving questions, denoted as S (S = questions-per-hour). During each hour, a participant can choose a section and
10 min read
Find minimum subarray length to reduce frequency Given an array arr[] of length N and a positive integer k, the task is to find the minimum length of the subarray that needs to be removed from the given array such that the frequency of the remaining elements in the array is less than or equal to k. Examples: Input: n = 4, arr[] = {3, 1, 3, 6}, k =
10 min read
Maximize minimum sweetness in cake cutting Given that cake consists of N chunks, whose individual sweetness is represented by the sweetness[] array, the task is to cut the cake into K + 1 pieces to maximize the minimum sweetness. Examples: Input: N = 6, K = 2, sweetness[] = {6, 3, 2, 8, 7, 5}Output: 9Explanation: Divide the cake into [6, 3],
7 min read
Maximize minimum element of an Array using operations Given an array A[] of N integers and two integers X and Y (X ⤠Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ⤠Y. Examples: Input: N= 3, A[] = {1,
8 min read
Maximize the minimum element and return it Given an array A[] of size N along with W, K. We can increase W continuous elements by 1 where we are allowed to perform this operation K times, the task is to maximize the minimum element and return the minimum element after operations. Examples: Input: N = 6, K = 2, W = 3, A[] = {2, 2, 2, 2, 1, 1}
8 min read
Find the maximum value of the K-th smallest usage value in Array Given an array arr[] of size N and with integers M, K. You are allowed to perform an operation where you can increase the value of the least element of the array by 1. You are to do this M times. The task is to find the largest possible value for the Kth smallest value among arr[] after M operations
7 min read
Aggressive Cows Given an array stalls[] which denotes the position of a stall and an integer k which denotes the number of aggressive cows. The task is to assign stalls to k cows such that the minimum distance between any two of them is the maximum possible.Examples: Input: stalls[] = [1, 2, 4, 8, 9], k = 3Output:
15+ min read
Minimum time to complete at least K tasks when everyone rest after each task Given an array arr[] of size n representing the time taken by a person to complete a task. Also, an array restTime[] which denotes the amount of time one person takes to rest after finishing a task. Each person is independent of others i.e. they can work simultaneously on different tasks at the same
8 min read
Maximum count of integers to be chosen from given two stacks having sum at most K Given two stacks stack1[] and stack2[] of size N and M respectively and an integer K, The task is to count the maximum number of integers from two stacks having sum less than or equal to K. Examples: Input: stack1[ ] = { 60, 90, 120 }stack2[ ] = { 100, 10, 10, 250 }, K = 130Output: 3Explanation: Tak
9 min read