Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
Last Updated :
07 Sep, 2021
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 generate at most D partitions, then check for a median lesser than T.
- Otherwise, check for a median greater than the current median T.
- Return the possible value of T at the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool possible( int T, int arr[], int n, int d)
{
int partition = 1;
int total = 0;
for ( int i = 0; i < n; i++) {
total = total + arr[i];
if (total > T) {
partition = partition + 1;
total = arr[i];
if (partition > d) {
return false ;
}
}
}
return true ;
}
void calcT( int n, int d, int arr[])
{
int mx = -1, sum = 0;
for ( int i = 0; i < n; i++) {
mx = max(mx, arr[i]);
sum = sum + arr[i];
}
int lb = mx;
int ub = sum;
while (lb < ub) {
int T_mid = lb + (ub - lb) / 2;
if (possible(T_mid, arr, n, d) == true ) {
ub = T_mid;
}
else {
lb = T_mid + 1;
}
}
cout << lb << endl;
}
int main()
{
int d = 2;
int arr[] = { 1, 1, 1, 1, 1 };
int n = sizeof arr / sizeof arr[0];
calcT(n, d, arr);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
public static boolean possible( int T, int arr[],
int n, int d)
{
int partition = 1 ;
int total = 0 ;
for ( int i = 0 ; i < n; i++)
{
total = total + arr[i];
if (total > T)
{
partition = partition + 1 ;
total = arr[i];
if (partition > d)
{
return false ;
}
}
}
return true ;
}
public static void calcT( int n, int d,
int arr[])
{
int mx = - 1 , sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
mx = Math.max(mx, arr[i]);
sum = sum + arr[i];
}
int lb = mx;
int ub = sum;
while (lb < ub)
{
int T_mid = lb + (ub - lb) / 2 ;
if (possible(T_mid, arr, n, d) == true )
{
ub = T_mid;
}
else
{
lb = T_mid + 1 ;
}
}
System.out.println(lb);
}
public static void main(String args[])
{
int d = 2 ;
int arr[] = { 1 , 1 , 1 , 1 , 1 };
int n = arr.length;
calcT(n, d, arr);
}
}
|
Python3
def possible(T, arr, n, d):
partition = 1 ;
total = 0 ;
for i in range (n):
total = total + arr[i];
if (total > T):
partition = partition + 1 ;
total = arr[i];
if (partition > d):
return False ;
return True ;
def calcT(n, d, arr):
mx = - 1 ; sum = 0 ;
for i in range (n):
mx = max (mx, arr[i]);
sum = sum + arr[i];
lb = mx;
ub = sum ;
while (lb < ub):
T_mid = lb + (ub - lb) / / 2 ;
if (possible(T_mid, arr, n, d) = = True ):
ub = T_mid;
else :
lb = T_mid + 1 ;
print (lb);
if __name__ = = '__main__' :
d = 2 ;
arr = [ 1 , 1 , 1 , 1 , 1 ];
n = len (arr);
calcT(n, d, arr);
|
C#
using System;
class GFG{
public static bool possible( int T, int []arr,
int n, int d)
{
int partition = 1;
int total = 0;
for ( int i = 0; i < n; i++)
{
total = total + arr[i];
if (total > T)
{
partition = partition + 1;
total = arr[i];
if (partition > d)
{
return false ;
}
}
}
return true ;
}
public static void calcT( int n, int d,
int []arr)
{
int mx = -1, sum = 0;
for ( int i = 0; i < n; i++)
{
mx = Math.Max(mx, arr[i]);
sum = sum + arr[i];
}
int lb = mx;
int ub = sum;
while (lb < ub)
{
int T_mid = lb + (ub - lb) / 2;
if (possible(T_mid, arr, n, d) == true )
{
ub = T_mid;
}
else
{
lb = T_mid + 1;
}
}
Console.WriteLine(lb);
}
public static void Main(String []args)
{
int d = 2;
int []arr = { 1, 1, 1, 1, 1 };
int n = arr.Length;
calcT(n, d, arr);
}
}
|
Javascript
<script>
function possible(T, arr,
n, d)
{
let partition = 1;
let total = 0;
for (let i = 0; i < n; i++)
{
total = total + arr[i];
if (total > T)
{
partition = partition + 1;
total = arr[i];
if (partition > d)
{
return false ;
}
}
}
return true ;
}
function calcT(n, d, arr)
{
let mx = -1, sum = 0;
for (let i = 0; i < n; i++)
{
mx = Math.max(mx, arr[i]);
sum = sum + arr[i];
}
let lb = mx;
let ub = sum;
while (lb < ub)
{
let T_mid = lb + (ub - lb) / 2;
if (possible(T_mid, arr, n, d) == true )
{
ub = T_mid;
}
else
{
lb = T_mid + 1;
}
}
document.write(lb);
}
let d = 2;
let arr = [ 1, 1, 1, 1, 1 ];
let n = arr.length;
calcT(n, d, arr);
</script>
|
Time complexity: O( N*log(sum) )
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...