human
Check if given array can be rearranged such that mean is equal to median | Function to return true or false if size of array is odd.
To prevent overflow.
If element is greater than mid , then it can only be present in right subarray.
If element is smaller than mid , then it can only be present in left subarray.
Else the element is present at the middle then return 1.
when element is not present in array then return 0.
Function to return true or false if size of array is even.
Calculating Candidate Median.
If Candidate Median if greater than Mean then decrement j.
If Candidate Median if less than Mean then increment i.
If Candidate Median if equal to Mean then return 1.
when No candidate found for mean.
Function to return true if Mean can be equal to any candidate median otherwise return false.
Calculating Mean.
If N is Odd.
If N is even.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to check if given array can be rearranged such that mean is equal to median and the other steps required.
 def binarySearch ( arr , size , key ) :
	low = 0
	high = size - 1
	while ( low <= high ) :
		mid = low + ( high - low ) // 2
		if ( key > arr [ mid ] ) :
			low = mid + 1
		elif ( key < arr [ mid ] ) :
			high = mid - 1
		else :
			return 1
	return 0
def twoPointers ( arr , N , mean ) :
	i = 0
	j = N - 1
	while ( i < j ) :
		temp = ( arr [ i ] + arr [ j ] ) / 2
		if ( temp > mean ) :
			j = j - 1
		elif ( temp < mean ) :
			i = i + 1
		else :
			return 1
	return 0
def checkArray ( arr , N ) :
	sum = 0
	for i in range ( 0 , N ) :
		sum += arr [ i ]
	mean = sum / N
	if ( N & 1 ) :
		return binarySearch ( arr , N , mean )
	else :
		return twoPointers ( arr , N , mean )
if __name__ == " _ _ main _ _ " :
	arr = [ 1.0 , 3.0 , 6.0 , 9.0 , 12.0 , 32.0 ]
	N = len ( arr )
	if ( checkArray ( arr , N ) ) :
		print ( " Yes " )
	else :
		print ( " No " )