human
What is a code to minimum removal of elements from end of an array required to obtain sum K and the other steps required given the following:
Minimum removal of elements from end of an array required to obtain sum K | Function to find the minimum number of elements required to be removed from the ends of an array to obtain a sum K.
Number of elements removed from the left and right ends of the array.
Sum of left and right subarrays.
No element is taken from left initially.
Start taking elements from right side.
( left + 1 ) : Count of elements removed from the left ( N - right ) : Count of elements removed from the right.
If sum is greater than K.
If it is not possible to obtain sum K.
Driver Code.
Given Array.
Given target sum"
Solution in Python:
assistant
#Here is code to minimum removal of elements from end of an array required to obtain sum K and the other steps required.
 def minSizeArr ( A , N , K ) :
	leftTaken = N
	rightTaken = N
	leftSum = 0
	rightSum = 0
	for left in range ( - 1 , N ) :
		if ( left != - 1 ) :
			leftSum += A [ left ]
		rightSum = 0
		for right in range ( N - 1 , left , - 1 ) :
			rightSum += A [ right ]
			if ( leftSum + rightSum == K ) :
				if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) :
					leftTaken = left + 1
					rightTaken = N - right
				break
			if ( leftSum + rightSum > K ) :
				break
	if ( leftTaken + rightTaken <= N ) :
		for i in range ( leftTaken ) :
			print ( A [ i ] , end = " ▁ " )
		for i in range ( rightTaken ) :
			print ( A [ N - i - 1 ] , end = " ▁ " )
	else :
		print ( - 1 )
if __name__ == " _ _ main _ _ " :
	N = 7
	A = [ 3 , 2 , 1 , 1 , 1 , 1 , 3 ]
	K = 10
	minSizeArr ( A , N , K )