Count the Hidden Sequences - Problem

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 we have that 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.

For example, given differences = [1, -3, 4], lower = 1, upper = 6, the hidden sequence is a sequence of length 4 whose elements are in between 1 and 6 (inclusive).

  • [3, 4, 1, 5] and [4, 5, 2, 6] are possible hidden sequences.
  • [5, 6, 3, 7] is not possible since it contains an element greater than 6.
  • [1, 2, 3, 4] is not possible since the differences are not correct.

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

Input & Output

Example 1 — Basic Case
$ Input: differences = [1,-3,4], lower = 1, upper = 6
Output: 2
💡 Note: Hidden sequences of length 4. Starting with 3: [3,4,1,5] ✓. Starting with 4: [4,5,2,6] ✓. Starting with 5: [5,6,3,7] ✗ (7 > 6). Valid sequences: 2
Example 2 — No Valid Sequences
$ Input: differences = [4,-7,2], lower = 3, upper = 6
Output: 0
💡 Note: Any sequence will go outside bounds. For example, start=3: [3,7,0,2] has 7>6 and 0<3. No valid starting position exists.
Example 3 — Single Valid Start
$ Input: differences = [1,-1,1], lower = 1, upper = 3
Output: 1
💡 Note: Only starting with 2 works: [2,3,2,3]. All values stay within [1,3]. Other starts go out of bounds.

Constraints

  • 1 ≤ differences.length ≤ 105
  • -105 ≤ differences[i] ≤ 105
  • -108 ≤ lower ≤ upper ≤ 108

Visualization

Tap to expand
Count the Hidden Sequences INPUT differences array: 1 idx 0 -3 idx 1 4 idx 2 Hidden sequence (n+1=4): h[0] h[1] h[2] h[3] +1 -3 +4 Constraints: lower=1 upper=6 All h[i] must be in [1,6] 1 6 ALGORITHM STEPS 1 Compute prefix sums prefix[i] = sum(diff[0..i-1]) prefix = [0, 1, -2, 2] 2 Find min/max prefix min_p = -2, max_p = 2 Range span = 2-(-2) = 4 3 Calculate valid h[0] h[0] + min_p >= lower h[0] + max_p <= upper 4 Count valid starts h[0] in [lower-min_p, upper-max_p] h[0] in [3, 4] count = upper - max_p - (lower - min_p) + 1 = 6-2 - (1-(-2)) + 1 = 2 FINAL RESULT Valid hidden sequences: h[0] = 3: [3, 4, 1, 5] h[0] = 4: [4, 5, 2, 6] Verification: [3,4,1,5]: 4-3=1, 1-4=-3, 5-1=4 All in [1,6] - OK [4,5,2,6]: 5-4=1, 2-5=-3, 6-2=4 All in [1,6] - OK OUTPUT 2 Key Insight: Once we fix h[0], all other values are determined by prefix sums: h[i] = h[0] + prefix[i]. The number of valid h[0] values equals (upper - max_prefix) - (lower - min_prefix) + 1, which simplifies to: upper - lower - (max_prefix - min_prefix) + 1. Time: O(n), Space: O(1). TutorialsPoint - Count the Hidden Sequences | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
28.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen