human
Write me a code to maximum sum subarray of size K with sum less than X and the other steps required given the following:
Maximum sum subarray of size K with sum less than X | Function to calculate maximum sum among all subarrays of size K with the sum less than X.
Initialize sum_K to 0.
Calculate sum of first K elements.
If sum_K is less than X.
Initialize MaxSum with sum_K.
Iterate over the array from ( K + 1 ) - th index.
Subtract the first element from the previous K elements and add the next element.
If sum_K is less than X.
Update the Max_Sum.
Driver Code.
Size of Array.
Function Call"
Solution in Python:
assistant
#Here is code to maximum sum subarray of size K with sum less than X and the other steps required.
 def maxSumSubarr ( A , N , K , X ) :
	sum_K = 0
	for i in range ( 0 , K ) :
		sum_K += A [ i ]
	Max_Sum = 0
	if ( sum_K < X ) :
		Max_Sum = sum_K
	for i in range ( K , N ) :
		sum_K -= ( A [ i - K ] - A [ i ] )
		if ( sum_K < X ) :
			Max_Sum = max ( Max_Sum , sum_K )
	print ( Max_Sum )
arr = [ - 5 , 8 , 7 , 2 , 10 , 1 , 20 , - 4 , 6 , 9 ]
K = 5
X = 30
N = len ( arr )
maxSumSubarr ( arr , N , K , X )