fork download
  1. # your code goes here
  2. #Find Sum of Range [l……….r] where(l<=r) using Prefix sum.
  3.  
  4. def func1(arr, l, r):
  5. s = 0
  6. for i in range(l, r+1):
  7. s+=arr[i]
  8. return s
  9.  
  10.  
  11. def func2(arr):
  12. pfx_sum = []
  13.  
  14. s = 0
  15.  
  16. for n in arr:
  17. s += n
  18. pfx_sum.append(s)
  19.  
  20. return pfx_sum
  21.  
  22. nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  23.  
  24. print(func2(nums))
  25.  
Success #stdin #stdout 0.09s 14088KB
stdin
Standard input is empty
stdout
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]