human
Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible | Function to check if the array can be partitioned into atmost d subarray with sum atmost T.
Initial partition.
Current sum.
If current sum exceeds T.
Create a new partition.
If count of partitions exceed d.
Function to find the minimum possible value of T.
Stores the maximum and total sum of elements.
Maximum element.
Sum of all elements.
Calculate median T.
If atmost D partitions possible.
Check for smaller T.
Otherwise.
Check for larger T.
Print the minimum T required.
Driver code.
Function call"
How can the above be solved in Python?
assistant
#Here is code to minimum possible value T such that at most D Partitions of the Array having at most sum T is possible and the other steps required.
 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 ) ;