human
Write me a code to minimum K such that sum of array elements after division by K does not exceed S and the other steps required given the following:
Minimum K such that sum of array elements after division by K does not exceed S | Function to return the minimum value of k that satisfies the given condition.
Find the maximum element.
Lowest answer can be 1 and the highest answer can be ( maximum + 1 ).
Binary search.
Get the mid element.
Calculate the sum after dividing the array by new K which is mid.
Search in the second half.
First half.
Driver code"
Solution in Python:
assistant
#Here is code to minimum K such that sum of array elements after division by K does not exceed S and the other steps required.
 def findMinimumK ( a , n , s ) :
	maximum = a [ 0 ]
	for i in range ( n ) :
		maximum = max ( maximum , a [ i ] )
	low = 1
	high = maximum + 1
	ans = high
	while ( low <= high ) :
		mid = ( low + high ) // 2
		sum = 0
		for i in range ( n ) :
			sum += ( a [ i ] // mid )
		if ( sum > s ) :
			low = mid + 1
		else :
			ans = min ( ans , mid )
			high = mid - 1
	return ans
a = [ 10 , 7 , 8 , 10 , 12 , 19 ]
n = len ( a )
s = 27
print ( findMinimumK ( a , n , s ) )