Skip to main content
LeetCode 2145, Medium. Topics: Array, Prefix Sum. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

Last modified on October 12, 2026