Given an array arr[] consisting of N integers and an integer D, the task is to find the least integer T such that the entire array can be partitioned into at most D subarrays from the given array with sum atmost T.
Examples:
Input: D = 5, arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Output: 15
Explanation:
If T = 15, then 5 subarrays {{1, 2, 3, 4, 5}, {6, 7}, {8}, {9}, {10}}Input: D = 2, arr[] = {1, 1, 1, 1, 1}
Output: 3
Explanation:
If T = 3, then the 2 partitions are {{1, 1, 1}, {1, 1}}
Naive Approach: The idea is to check for all possible values of T in the range [max(element), sum(element)] whether it is possible to have at most D partition.
Time Complexity: O( N*R )
Auxiliary Space: O(1)
Efficient Approach: The idea is to use Binary search to optimize the above approach. Follow the steps below to solve the problem:
- Consider T in the range R = [ max(element), sum(element) ].
- If median T can generates at most D partitions, then check for a median lesser than T.
- Otherwise, check for a median greater than current median T.
- Return the possible value of T at the end.
Below is the implementation of the above approach:
C++
// C++ Program for the above approach #include <bits/stdc++.h> using namespace std; // Funtion to check if the array // can be partitioned into atmost d // subarray with sum atmost T bool possible(int T, int arr[], int n, int d) { // Initial partition int partition = 1; // Current sum int total = 0; for (int i = 0; i < n; i++) { total = total + arr[i]; // If current sum exceeds T if (total > T) { // Create a new partition partition = partition + 1; total = arr[i]; // If count of partitions // exceed d if (partition > d) { return false; } } } return true; } // Function to find the minimum // possible value of T void calcT(int n, int d, int arr[]) { // Stores the maximum and // total sum of elements int mx = -1, sum = 0; for (int i = 0; i < n; i++) { // Maximum element mx = max(mx, arr[i]); // Sum of all elements sum = sum + arr[i]; } int lb = mx; int ub = sum; while (lb < ub) { // Calculate median T int T_mid = lb + (ub - lb) / 2; // If atmost D partitions possible if (possible(T_mid, arr, n, d) == true) { // Check for smaller T ub = T_mid; } // Otherwise else { // Check for larger T lb = T_mid + 1; } } // Print the minimum T required cout << lb << endl; } // Driver Code int main() { int d = 2; int arr[] = { 1, 1, 1, 1, 1 }; int n = sizeof arr / sizeof arr[0]; // Function call calcT(n, d, arr); return 0; } |
Java
// Java program for the above approach import java.util.*; import java.io.*; class GFG{ // Function to check if the array // can be partitioned into atmost d // subarray with sum atmost T public static boolean possible(int T, int arr[], int n, int d) { // Initial partition int partition = 1; // Current sum int total = 0; for(int i = 0; i < n; i++) { total = total + arr[i]; // If current sum exceeds T if (total > T) { // Create a new partition partition = partition + 1; total = arr[i]; // If count of partitions // exceed d if (partition > d) { return false; } } } return true; } // Function to find the minimum // possible value of T public static void calcT(int n, int d, int arr[]) { // Stores the maximum and // total sum of elements int mx = -1, sum = 0; for(int i = 0; i < n; i++) { // Maximum element mx = Math.max(mx, arr[i]); // Sum of all elements sum = sum + arr[i]; } int lb = mx; int ub = sum; while (lb < ub) { // Calculate median T int T_mid = lb + (ub - lb) / 2; // If atmost D partitions possible if (possible(T_mid, arr, n, d) == true) { // Check for smaller T ub = T_mid; } // Otherwise else { // Check for larger T lb = T_mid + 1; } } // Print the minimum T required System.out.println(lb); } // Driver code public static void main(String args[]) { int d = 2; int arr[] = { 1, 1, 1, 1, 1 }; int n = arr.length; // Function call calcT(n, d, arr); } } // This code is contributed by decoding |
Python3
# Python3 program for the above approach # Function to check if the array # can be partitioned into atmost d # subarray with sum atmost T def possible(T, arr, n, d): # Initial partition partition = 1; # Current sum total = 0; for i in range(n): total = total + arr[i]; # If current sum exceeds T if (total > T): # Create a new partition partition = partition + 1; total = arr[i]; # If count of partitions # exceed d if (partition > d): return False; return True; # Function to find the minimum # possible value of T def calcT(n, d, arr): # Stores the maximum and # total sum of elements mx = -1; sum = 0; for i in range(n): # Maximum element mx = max(mx, arr[i]); # Sum of all elements sum = sum + arr[i]; lb = mx; ub = sum; while (lb < ub): # Calculate median T T_mid = lb + (ub - lb) // 2; # If atmost D partitions possible if (possible(T_mid, arr, n, d) == True): # Check for smaller T ub = T_mid; # Otherwise else: # Check for larger T lb = T_mid + 1; # Print the minimum T required print(lb); # Driver code if __name__ == '__main__': d = 2; arr = [ 1, 1, 1, 1, 1 ]; n = len(arr); # Function call calcT(n, d, arr); # This code is contributed by Rajput-Ji |
C#
// C# program for the above approach using System; class GFG{ // Function to check if the array // can be partitioned into atmost d // subarray with sum atmost T public static bool possible(int T, int []arr, int n, int d) { // Initial partition int partition = 1; // Current sum int total = 0; for(int i = 0; i < n; i++) { total = total + arr[i]; // If current sum exceeds T if (total > T) { // Create a new partition partition = partition + 1; total = arr[i]; // If count of partitions // exceed d if (partition > d) { return false; } } } return true; } // Function to find the minimum // possible value of T public static void calcT(int n, int d, int []arr) { // Stores the maximum and // total sum of elements int mx = -1, sum = 0; for(int i = 0; i < n; i++) { // Maximum element mx = Math.Max(mx, arr[i]); // Sum of all elements sum = sum + arr[i]; } int lb = mx; int ub = sum; while (lb < ub) { // Calculate median T int T_mid = lb + (ub - lb) / 2; // If atmost D partitions possible if (possible(T_mid, arr, n, d) == true) { // Check for smaller T ub = T_mid; } // Otherwise else { // Check for larger T lb = T_mid + 1; } } // Print the minimum T required Console.WriteLine(lb); } // Driver code public static void Main(String []args) { int d = 2; int []arr = { 1, 1, 1, 1, 1 }; int n = arr.Length; // Function call calcT(n, d, arr); } } // This code is contributed by 29AjayKumar |
3
Time complexity: O( N*log(sum) )
Auxiliary Space: O(1)
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.
Recommended Posts:
- Partitions possible such that the minimum element divides all the other elements of the partition
- Minimum partitions of maximum size 2 and sum limited by given value
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Maximize partitions such that no two substrings have any common character
- Split array into minimum number of subsets having maximum pair sum at most K
- Find the maximum possible value of the minimum value of modified array
- Generate all unique partitions of an integer
- Maximum number of partitions that can be sorted individually to make sorted
- Count of multiplicative partitions of N
- Count numbers present in partitions of N
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Minimum possible value of max(A, B) such that LCM(A, B) = C
- Minimum possible sum of array B such that AiBi = AjBj for all 1 ≤ i < j ≤ N
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

