human
Find the modified array after performing k operations of given type | Utility function to print the contents of an array.
Function to remove the minimum value of the array from every element of the array.
Get the minimum value from the array.
Remove the minimum value from every element of the array.
Function to remove every element of the array from the maximum value of the array.
Get the maximum value from the array.
Remove every element of the array from the maximum value of the array.
Function to print the modified array after k operations.
If k is odd then remove the minimum element of the array from every element of the array.
Else remove every element of the array from the maximum value from the array.
Print the modified array.
Driver code"
Solution in Python:
assistant
#Here is code to find the modified array after performing k operations of given type and the other steps required.
 def printArray ( arr , n ) :
	for i in range ( n ) :
		print ( arr [ i ] , end = " ▁ " ) ;
def removeMin ( arr , n ) :
	minVal = arr [ 0 ] ;
	for i in range ( 1 , n ) :
		minVal = min ( minVal , arr [ i ] ) ;
	for i in range ( n ) :
		arr [ i ] = arr [ i ] - minVal ;
def removeFromMax ( arr , n ) :
	maxVal = arr [ 0 ] ;
	for i in range ( 1 , n ) :
		maxVal = max ( maxVal , arr [ i ] ) ;
	for i in range ( n ) :
		arr [ i ] = maxVal - arr [ i ] ;
def modifyArray ( arr , n , k ) :
	if ( k % 2 == 0 ) :
		removeMin ( arr , n ) ;
	else :
		removeFromMax ( arr , n ) ;
	printArray ( arr , n ) ;
if __name__ == " _ _ main _ _ " :
	arr = [ 4 , 8 , 12 , 16 ] ;
	n = len ( arr )
	k = 2 ;
	modifyArray ( arr , n , k ) ;