> ## 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.

# Count the Hidden Sequences Python Solution

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

LeetCode 2145, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/count-the-hidden-sequences/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 2145   # by problem number
lcpy gen -s count_the_hidden_sequences   # by problem name
```

## Problem

You are given a 0-indexed array `differences` of `n` integers, which describes the differences between each pair of consecutive integers of a hidden sequence of length `n + 1`. You are also given two integers `lower` and `upper` that describe the inclusive range of values that the hidden sequence can contain. Return the number of possible hidden sequences. If there are no possible sequences, return 0.

### Examples

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
Input: differences = [1,-3,4], lower = 1, upper = 6
Output: 2
Explanation: The possible hidden sequences are:
- [3, 4, 1, 5]
- [4, 5, 2, 6]
```

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
Input: differences = [3,-4,5,1,-2], lower = -4, upper = 5
Output: 4
```

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
Input: differences = [4,-7,2], lower = 3, upper = 6
Output: 0
```

### Constraints

* `n == differences.length`
* `1 <= n <= 10^5`
* `-10^5 <= differences[i] <= 10^5`
* `-10^5 <= lower <= upper <= 10^5`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def number_of_arrays(self, differences: list[int], lower: int, upper: int) -> int:
        offset = 0
        min_offset = 0
        max_offset = 0

        for difference in differences:
            offset += difference
            min_offset = min(min_offset, offset)
            max_offset = max(max_offset, offset)

        possible = (upper - lower) - (max_offset - min_offset) + 1
        return max(0, possible)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


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