VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-time-to-complete-at-least-k-tasks-when-everyone-rest-after-each-task/

⇱ Minimum time to complete at least K tasks when everyone rest after each task - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum time to complete at least K tasks when everyone rest after each task

Last Updated : 23 Jul, 2025

Given an arrayarr[] 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.


Output
5

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.


Comment
Article Tags:
Article Tags: