human
Minimum removal of elements from end of an array required to obtain sum K | Function to find the smallest array that can be removed from the ends of an array to obtain sum K.
Sum of complete array.
If given number is greater than sum of the array.
If number is equal to the sum of array.
tar is sum of middle subarray.
Find the longest subarray with sum equal to tar.
If there is no subarray with sum equal to tar.
Driver Code.
Given Array.
Given target sum"
Solution in Python:
 ... Write me a code to minimum removal of elements from end of an array required to obtain sum K and the other steps required?
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 ) :
	sum = 0
	for i in range ( N ) :
		sum += A [ i ]
	if ( K > sum ) :
		print ( - 1 ) ;
		return
	if ( K == sum ) :
		for i in range ( N ) :
			print ( A [ i ] , end = " ▁ " )
		return
	tar = sum - K
	um = { }
	um [ 0 ] = - 1
	left = 0
	right = 0
	cur = 0
	maxi = - 1
	for i in range ( N ) :
		cur += A [ i ]
		if ( ( cur - tar ) in um and ( i - um [ cur - tar ] ) > maxi ) :
			maxi = i - um [ cur - tar ]
			right = i
			left = um [ cur - tar ]
		if ( cur not in um ) :
			um [ cur ] = i
	if ( maxi == - 1 ) :
		print ( - 1 )
	else :
		for i in range ( left + 1 ) :
			print ( A [ i ] , end = " ▁ " )
		for i in range ( right ) :
			print ( A [ N - i - 1 ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	N = 7
	A = [ 3 , 2 , 1 , 1 , 1 , 1 , 3 ]
	K = 10
	minSizeArr ( A , N , K )