Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Google
Count The Hidden Sequences

Count how many integer sequences can produce a given array of adjacent differences while staying within a fixed value range.

Acceptance 0%
Problem Statement

You are given an integer array differences of length n and two integers lower and upper.

Consider an integer sequence hidden of length n + 1 such that for every index i in [0, n - 1]:

hidden[i+1]−hidden[i]=differences[i]hidden[i + 1] - hidden[i] = differences[i]

Your task is to count how many such sequences exist if every value in hidden must lie within the inclusive range [lower, upper].

Return the number of valid sequences.

A sequence is valid if all its values are integers and each value is between lower and upper, inclusive.

Input Format

  • An integer array differences.
  • Two integers lower and upper.

The sequence values are constrained to be integers.

Output Format

  • Return a single integer: the number of valid hidden sequences.

Constraints

  • 1 <= differences.length <= $10^{5}$
  • $-10^{5}$ <= differences[i] <= $10^{5}$
  • $-10^{5}$ <= lower <= upper <= $10^{5}$
  • All sequence values must be integers and lie in [lower, upper].
  • The answer fits in a 32-bit signed integer.
Examples
Sample cases returned by the problem API.

Example 1

Input

differences = [3,-4,5,1,-2], lower = -4, upper = 5

Output

4

Explanation

The cumulative offsets determine a range of allowed starting values. Exactly 4 integer starting values keep every element within [-4, 5].

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.