human
Range queries for alternatively addition and subtraction on given Array | Function to find the result of alternatively adding and subtracting elements in the range [ L , R ].
A boolean variable flag to alternatively add and subtract.
Iterate from [ L , R ].
If flag is False , then add & toggle the flag.
If flag is True subtract and toggle the flag.
Return the final result.
Function to find the value for each query.
Iterate for each query.
Driver Code.
Given array.
Given Queries.
Function Call"
Solution in Python:
 ... Write me a code to range queries for alternatively addition and subtraction on given Array and the other steps required?
assistant
#Here is code to range queries for alternatively addition and subtraction on given Array and the other steps required.
 def findResultUtil ( arr , L , R ) :
	result = 0
	flag = False
	for i in range ( L , R + 1 ) :
		if ( flag == False ) :
			result = result + arr [ i ]
			flag = True
		else :
			result = result - arr [ i ]
			flag = False
	return result
def findResult ( arr , n , q , m ) :
	for i in range ( m ) :
		print ( findResultUtil ( arr , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 ]
	n = len ( arr )
	q = [ [ 2 , 5 ] , [ 6 , 8 ] , [ 1 , 7 ] , [ 4 , 8 ] , [ 0 , 5 ] ]
	m = len ( q )
	findResult ( arr , n , q , m )