Given a positive integer K and an array arr[] consisting of N positive integers, such that arr[i] is the number of processes ith processor can schedule in 1 second. The task is to minimize the total time required to schedule K processes such that after scheduling by the ith processor, arr[i] is reduced to floor(arr[i]/2).
Examples:
Input: N = 5, arr[] = {3, 1, 7, 2, 4}, K = 15
Output: 4
Explanation:
The order of scheduled process are as follows:
- The 3rd process is scheduled first. The array arr[] modifies to {3, 1, 3, 2, 4}, as arr[2] = floor(arr[2] / 2) = floor(7 / 2) = 3.
- The 5th process is scheduled next. The array arr[] modifies to {3, 1, 3, 2, 2}.
- The 1st process is scheduled next. The array arr[] modifies to {1, 1, 3, 2, 2}.
- The 2nd process is scheduled next. The array arr[] modifies to {3, 0, 3, 2, 4}.
The total processes scheduled by all the process = 7 + 4 + 3 + 1 = 15(= K) and the total time required is 4 seconds.
Input: N = 4, arr[] = {1, 5, 8, 6}, K = 10
Output: 2
Naive Approach: The simplest approach to solve the given problem is to sort the given list in ascending order and choose the processor with the highest ability and reduce the value of K by that value and delete that processor from the list and add half of that in the sorted list again. Repeat the above process until at least K processes are scheduled and print the time required after scheduling at least K processes.
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by using the concept of Hashing. Follow the below steps to solve the problem:
- Initialize an auxiliary array tmp[] of the size of the maximum element present in the given array.
- Initialize a variable, say count to store the minimum time to schedule all processes respectively.
- Traverse the given array tmp[] from the end and perform the following steps:
- If the current element in tmp[] is greater than 0 and i * tmp[i] is smaller than K.
- Decrease the value of K by the value i * tmp[i].
- Increase tmp[i/2] by tmp[i] as the ability of the processor will decrease by half.
- Increase the value of count by the value tmp[i].
- If the value of K is already smaller than or equal to 0, then print the value of count as the result.
- If the current element in the array tmp[] is at least 0 and the value of i * tmp[i] is at least K, then perform the following steps:
- If K is divisible by the current index, then increment the value of count by K / i.
- Otherwise, increment the value of count by K/i +1.
- If the current element in tmp[] is greater than 0 and i * tmp[i] is smaller than K.
- After completing the above steps, print -1 if it is not possible to schedule all processes. Otherwise, print the count as the minimum time required.
Below is the implementation of the above approach:
Java
// Java program for the above approach import java.util.*;import java.lang.*; class GFG { // Function to find minimum required // time to schedule all process static int minTime(int[] A, int n, int K) { // Stores max element from A[] int max_ability = A[0]; // Find the maximum element for (int i = 1; i < n; i++) { max_ability = Math.max( max_ability, A[i]); } // Stores frequency of each element int tmp[] = new int[max_ability + 1]; // Stores minimum time required // to schedule all process int count = 0; // Count frquencies of elements for (int i = 0; i < n; i++) { tmp[A[i]]++; } // Find the minimum time for (int i = max_ability; i >= 0; i--) { if (tmp[i] != 0) { if (tmp[i] * i < K) { // Decrease the value // of K K -= (i * tmp[i]); // Increment tmp[i/2] tmp[i / 2] += tmp[i]; // Increment the count count += tmp[i]; // Return count, if all // process are scheduled if (K <= 0) { return count; } } else { // Increment count if (K % i != 0) { count += (K / i) + 1; } else { count += (K / i); } // Return the count return count; } } } // If it is not possible to // schedule all process return -1; } // Driver Code public static void main(String[] args) { int arr[] = { 3, 1, 7, 2, 4 }; int N = arr.length; int K = 15; System.out.println( minTime(arr, N, K)); }} |
4
Time Complexity: O(M), where M is the maximum element in the array.
Auxiliary Space: O(M)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


