human
Find the minimum and maximum amount to buy all N candies | Function to find the minimum amount to buy all candies.
Buy current candy.
And take k candies for free from the last.
Function to find the maximum amount to buy all candies.
Buy candy with maximum amount.
And get k candies for free from the starting.
Driver code.
Function call"
How can the above be solved in Python?
assistant
#Here is code to find the minimum and maximum amount to buy all N candies and the other steps required.
 def findMinimum ( arr , n , k ) :
	res = 0
	i = 0
	while ( n ) :
		res += arr [ i ]
		n = n - k
		i += 1
	return res
def findMaximum ( arr , n , k ) :
	res = 0
	index = 0
	i = n - 1
	while ( i >= index ) :
		res += arr [ i ]
		index += k
		i -= 1
	return res
arr = [ 3 , 2 , 1 , 4 ]
n = len ( arr )
k = 2
arr . sort ( )
print ( findMinimum ( arr , n , k ) , " ▁ " , findMaximum ( arr , n , k ) )