Click4Ai

17.

Easy

Write a function cumulative_sum(arr) that returns the cumulative (prefix) sum of an array. Each element at index i in the output is the sum of all elements from index 0 to i.

Example:

Input: arr = [1, 2, 3, 4, 5]

Output: [1, 3, 6, 10, 15]

**Explanation:** [1, 1+2, 1+2+3, 1+2+3+4, 1+2+3+4+5]

Constraints:

  • Array length: 1 <= len(arr) <= 1000
  • Do not use np.cumsum()
  • Test Cases

    Test Case 1
    Input: [1, 2, 3, 4, 5]
    Expected: [1, 3, 6, 10, 15]
    Test Case 2
    Input: [5, 5, 5]
    Expected: [5, 10, 15]
    Test Case 3
    Input: [10]
    Expected: [10]
    + 2 hidden test cases