human
Sum of elements of an AP in the given range | Function to find sum in the given range.
Find the value of k.
Find the common difference.
Find the sum.
Driver code"
How can the above be solved in Python?
 Give me a code to sum of elements of an AP in the given range and the other steps required?
assistant
#Here is code to sum of elements of an AP in the given range and the other steps required.
 def findSum ( arr , n , left , right ) :
	k = right - left ;
	d = arr [ 1 ] - arr [ 0 ] ;
	ans = arr [ left - 1 ] * ( k + 1 ) ;
	ans = ans + ( d * ( k * ( k + 1 ) ) ) // 2 ;
	return ans ;
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] ;
	queries = 3 ;
	q = [ [ 2 , 4 ] , [ 2 , 6 ] , [ 5 , 6 ] ] ;
	n = len ( arr ) ;
	for i in range ( queries ) :
		print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) ;