human
Subarray whose absolute sum is closest to K | function to return the index.
Add Last element tp currSum.
Save Difference of previous Iteration.
Calculate new Difference.
When the Sum exceeds K.
Current Difference greater in magnitude Store Temporary Result.
Difference in Previous was lesser In previous , Right index = j - 1.
In next iteration , Left Index Increases but Right Index remains the Same Update currSum and i Accordingly.
Case to simply increase Right Index.
Check if lesser deviation found.
Driver Code"
Solution in Python:
 ... Can you provide a code to subarray whose absolute sum is closest to K and the other steps required?
assistant
#Here is code to subarray whose absolute sum is closest to K and the other steps required.
 def getSubArray ( arr , n , K ) :
	currSum = 0
	prevDif = 0
	currDif = 0
	result = [ - 1 , - 1 , abs ( K - abs ( currSum ) ) ]
	resultTmp = result
	i = 0
	j = 0
	while ( i <= j and j < n ) :
		currSum += arr [ j ]
		prevDif = currDif
		currDif = K - abs ( currSum )
		if ( currDif <= 0 ) :
			if abs ( currDif ) < abs ( prevDif ) :
				resultTmp = [ i , j , currDif ]
			else :
				resultTmp = [ i , j - 1 , prevDif ]
			currSum -= ( arr [ i ] + arr [ j ] )
			i += 1
		else :
			resultTmp = [ i , j , currDif ]
			j += 1
		if ( abs ( resultTmp [ 2 ] ) < abs ( result [ 2 ] ) ) :
			result = resultTmp
	return result
def main ( ) :
	arr = [ 15 , - 3 , 5 , 2 , 7 , 6 , 34 , - 6 ]
	n = len ( arr )
	K = 50
	[ i , j , minDev ] = getSubArray ( arr , n , K )
	if ( i == - 1 ) :
		print ( " The ▁ empty ▁ array ▁ shows ▁ minimum ▁ Deviation " )
		return 0
	for i in range ( i , j + 1 ) :
		print arr [ i ] ,
main ( )