human
Can you provide a code to sum of elements in an array whose difference with the mean of another array is less than k and the other steps required given the following:
Sum of elements in an array whose difference with the mean of another array is less than k | Function for finding sum of elements whose diff with mean is not more than k.
Find the mean of second array.
Find sum of elements from array1 whose difference with mean is not more than k.
Return result.
Driver code"
Solution in Python:
assistant
#Here is code to sum of elements in an array whose difference with the mean of another array is less than k and the other steps required.
 def findSumofEle ( arr1 , m , arr2 , n , k ) :
	arraySum = 0
	for i in range ( n ) :
		arraySum += arr2 [ i ]
	mean = arraySum / n
	sumOfElements = 0
	difference = 0
	for i in range ( m ) :
		difference = arr1 [ i ] - mean
		if ( ( difference < 0 ) and ( k > ( - 1 ) * difference ) ) :
			sumOfElements += arr1 [ i ]
		if ( ( difference >= 0 ) and ( k > difference ) ) :
			sumOfElements += arr1 [ i ]
	return sumOfElements
arr1 = [ 1 , 2 , 3 , 4 , 7 , 9 ]
arr2 = [ 0 , 1 , 2 , 1 , 1 , 4 ]
k = 2
m = len ( arr1 )
n = len ( arr2 )
print ( findSumofEle ( arr1 , m , arr2 , n , k ) )