human
Queries to find maximum sum contiguous subarrays of given length in a rotating array | Function to calculate the maximum sum of length k.
Calculating the max sum for the first k elements.
Find subarray with maximum sum.
Update the sum.
Return maximum sum.
Function to calculate gcd of the two numbers n1 and n2.
Base Case.
Recursively find the GCD.
Function to rotate the array by Y.
For handling k >= N.
Dividing the array into number of sets.
Rotate the array by Y.
Update arr [ j ].
Return the rotated array.
Function that performs the queries on the given array.
Traverse each query.
If query of type X = 1.
Print the array.
If query of type X = 2.
Driver Code.
Given array arr [ ].
Given Queries.
Function call"
Solution in Python:
assistant
#Here is code to queries to find maximum sum contiguous subarrays of given length in a rotating array and the other steps required.
 def MaxSum ( arr , n , k ) :
	i , max_sum = 0 , 0
	sum = 0
	while i < k :
		sum += arr [ i ]
		i += 1
	max_sum = sum
	while ( i < n ) :
		sum = sum - arr [ i - k ] + arr [ i ]
		if ( max_sum < sum ) :
			max_sum = sum
		i += 1
	return max_sum
def gcd ( n1 , n2 ) :
	if ( n2 == 0 ) :
		return n1
	else :
		return gcd ( n2 , n1 % n2 )
def RotateArr ( arr , n , d ) :
	i = 0
	j = 0
	d = d % n
	no_of_sets = gcd ( d , n )
	for i in range ( no_of_sets ) :
		temp = arr [ i ]
		j = i
		while ( True ) :
			k = j + d
			if ( k >= n ) :
				k = k - n
			if ( k == i ) :
				break
			arr [ j ] = arr [ k ]
			j = k
		arr [ j ] = temp
	return arr
def performQuery ( arr , Q , q ) :
	N = len ( arr )
	for i in range ( q ) :
		if ( Q [ i ] [ 0 ] == 1 ) :
			arr = RotateArr ( arr , N , Q [ i ] [ 1 ] )
			for t in arr :
				print ( t , end = " ▁ " )
			print ( )
		else :
			print ( MaxSum ( arr , N , Q [ i ] [ 1 ] ) )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 3 , 4 , 5 ]
	q = 5
	Q = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 1 , 3 ] , [ 1 , 1 ] , [ 2 , 4 ] ]
	performQuery ( arr , Q , q )