Menu Close

Given an integer array nums…sum,update[FTC]

Multiple Queries using array nums
Given an integer array nums, handle multiple queries of the following types:
Update the value of an element in nums.
Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.

Input
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]

Output
[null, 9, null, 8]
class NumArr(object):
	def __init__(self,nums):
		self.nums=nums
	def update(self,index,val):
		self.nums[index]=val
	def sum1(self,i,j):
		self.sum=0
		for k in range(i,j+1):
			self.sum+=self.nums[k]
		return self.sum

#getting the input
numarr,sumr= list(map(int,input('Num : ').split(','))),list(map(int,input('sumR : ').split(',')))
update=list(map(int,input('Update : ').split(',')))
rang=list(map(int,input('SRange : ').split(',')))

#creating obj 
obj1=NumArr(numarr)
x=obj1.sum1(rang[0],rang[1])
obj1.update(update[0],update[1])
y=obj1.sum1(rang[0],rang[1])
print([None,x,None,y])


Input_1:
Num : 1,3,5
sumR : 0,2
Update : 1,2
SRange : 0,2

Output:
[None, 9, None, 8]


Input_2:
Num : 1,2,3,5,6,7,8
sumR : 0,5
Update : 5,10
SRange : 0,6

Output:
[None, 32, None, 35]


Illustration of the Output:

Executed using python3