Mid/SeniorArrayPrefix Sum

Count the Hidden Sequences

You are given a 0-indexed array of n integers differences, which describes the differences between each pair of consecutive integers of a hidden sequence of length (n + 1). More formally, call the hidden sequence hidden; then differences[i] = hidden[i + 1] - hidden[i].

You are further given two integers lower and upper that describe the inclusive range of values [lower, upper] that the hidden sequence can contain.

Return the number of possible hidden sequences there are. If there are no possible sequences, return 0.

Example 1
Inputdifferences = [1,-3,4], lower = 1, upper = 6
Output2
The possible hidden sequences are [3, 4, 1, 5] and [4, 5, 2, 6], so the answer is 2.
Example 2
Inputdifferences = [3,-4,5,1,-2], lower = -4, upper = 5
Output4
There are four possible hidden sequences: [-3, 0, -4, 1, 2, 0], [-2, 1, -3, 2, 3, 1], [-1, 2, -2, 3, 4, 2], and [0, 3, -1, 4, 5, 3].

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate