> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Range Sum Query - Immutable Python Solution

> Tested Python solution for LeetCode 303 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 303, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Design](/catalog/topics/design), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/range-sum-query-immutable/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 303   # by problem number
lcpy gen -s range_sum_query_immutable   # by problem name
```

## Problem

Given an integer array `nums`, handle multiple queries of the following type:

1. Calculate the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** where `left <= right`.

Implement the `NumArray` class:

* `NumArray(int[] nums)` Initializes the object with the integer array `nums`.
* `int sumRange(int left, int right)` Returns the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** (i.e. `nums[left] + nums[left + 1] + ... + nums[right]`).

### Examples

```
Input
['NumArray', 'sumRange', 'sumRange', 'sumRange']
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
Output
[None, 1, -1, -3]

Explanation
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
```

### Constraints

* 1 \<= nums.length \<= 10\<sup>4\</sup>
* -10\<sup>5\</sup> \<= nums\[i] \<= 10\<sup>5\</sup>
* 0 \<= left \<= right \< nums.length
* At most 10\<sup>4\</sup> calls will be made to `sumRange`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/range_sum_query_immutable/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/range_sum_query_immutable/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from itertools import accumulate


class NumArray:
    # Time: O(n) init, O(1) per query
    # Space: O(n)
    def __init__(self, nums: list[int]) -> None:
        self.prefix = [0, *accumulate(nums)]

    def sum_range(self, left: int, right: int) -> int:
        return self.prefix[right + 1] - self.prefix[left]
```

## Complexity

| Time | Space |
| - | - |
| O(n) init, O(1) per query | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.