human
Find K such that changing all elements of the Array greater than K to K will make array sum N | Function to return K such that changing all elements greater than K to K will make array sum N otherwise return - 1.
Sorting the array in increasing order.
Loop through all the elements of the array.
Checking if sum of array equals N.
Driver code"
Solution in Python:
assistant
#Here is code to find K such that changing all elements of the Array greater than K to K will make array sum N and the other steps required.
 def findK ( arr , size , N ) :
	arr = sorted ( arr )
	temp_sum = 0
	for i in range ( size ) :
		temp_sum += arr [ i ]
		if ( N - temp_sum == arr [ i ] * ( size - i - 1 ) ) :
			return arr [ i ]
	return - 1
arr = [ 3 , 1 , 10 , 4 , 8 ]
size = len ( arr )
N = 16
print ( findK ( arr , size , N ) )