human
Maximize median after doing K addition operation on the Array | Function to check operation can be perform or not.
Number of operation to perform s . t . mid is median.
If mid is median of the array.
Function to find max median of the array.
Lowest possible median.
Highest possible median.
Checking for mid is possible for the median of array after doing at most k operation.
Return the max possible ans.
Driver Code.
Given array.
Given number of operation.
Size of array.
Sort the array.
Function call"
How can the above be solved in Python?
 Write me a code to maximize median after doing K addition operation on the Array and the other steps required?
assistant
#Here is code to maximize median after doing K addition operation on the Array and the other steps required.
 def possible ( arr , N , mid , K ) :
	add = 0
	for i in range ( N // 2 - ( N + 1 ) % 2 , N ) :
		if ( mid - arr [ i ] > 0 ) :
			add += ( mid - arr [ i ] )
			if ( add > K ) :
				return False
	if ( add <= K ) :
		return True
	else :
		return False
def findMaxMedian ( arr , N , K ) :
	low = 1
	mx = 0
	for i in range ( N ) :
		mx = max ( mx , arr [ i ] )
	high = K + mx
	while ( low <= high ) :
		mid = ( high + low ) // 2
		if ( possible ( arr , N , mid , K ) ) :
			low = mid + 1
		else :
			high = mid - 1
	if ( N % 2 == 0 ) :
		if ( low - 1 < arr [ N // 2 ] ) :
			return ( arr [ N // 2 ] + low - 1 ) // 2
	return low - 1
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 3 , 6 ]
	K = 10
	N = len ( arr )
	arr = sorted ( arr )
	print ( findMaxMedian ( arr , N , K ) )