Count arrays with per-index upper bounds and no two equal neighbors

Quick Overview

Count integer arrays where each element lies between 1 and its own upper bound and no two neighbors are equal, modulo 10^9 + 7, for up to 10^5 positions and bounds up to 10^9. The problem tests dynamic programming over values and how to make each transition cheap when the value range is huge.

Count arrays with per-index upper bounds and no two equal neighbors

Company: Deshaw

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given an integer array `a` of length `n`, count the number of integer arrays `b` of the same length that satisfy both of the following conditions: - `1 <= b[i] <= a[i]` for every index `i`. - No two consecutive elements are equal, that is, `b[i] != b[i + 1]` for every valid `i`. Since the answer can be very large, return it modulo `10^9 + 7`. ### Function Signature ```python def count_valid_sequences(a: list[int]) -> int: ... ``` ### Constraints - `1 <= n <= 10^5` - `1 <= a[i] <= 10^9` - Return the answer modulo `10^9 + 7`. ### Examples ```text Input: a = [2, 2, 2] Output: 2 ``` The valid arrays are `[1, 2, 1]` and `[2, 1, 2]`. Any other choice puts two equal values next to each other. ```text Input: a = [3, 1, 3] Output: 4 ``` `b[1]` must be 1, so `b[0]` and `b[2]` each come from {2, 3} independently, which gives `2 * 2 = 4` arrays. ```text Input: a = [2, 3, 1] Output: 3 ``` `b[2]` must be 1, so `b[1]` is 2 or 3. With `b[1] = 2` only `b[0] = 1` works, and with `b[1] = 3` both 1 and 2 work for `b[0]`. The arrays are `[1, 2, 1]`, `[1, 3, 1]` and `[2, 3, 1]`.

Overview: Count integer arrays where each element lies between 1 and its own upper bound and no two neighbors are equal, modulo 10^9 + 7, for up to 10^5 positions and bounds up to 10^9. The problem tests dynamic programming over values and how to make each transition cheap when the value range is huge.

Deshaw logo
Deshaw
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array a of length n, count the number of integer arrays b of the same length that satisfy both of the following conditions:

  • 1 <= b[i] <= a[i] for every index i .
  • No two consecutive elements are equal, that is, b[i] != b[i + 1] for every valid i .

Since the answer can be very large, return it modulo 10^9 + 7.

Function Signature

def count_valid_sequences(a: list[int]) -> int:
    ...

Constraints

  • 1 <= n <= 10^5
  • 1 <= a[i] <= 10^9
  • Return the answer modulo 10^9 + 7 .

Examples

Input: a = [2, 2, 2]
Output: 2

The valid arrays are [1, 2, 1] and [2, 1, 2]. Any other choice puts two equal values next to each other.

Input: a = [3, 1, 3]
Output: 4

b[1] must be 1, so b[0] and b[2] each come from {2, 3} independently, which gives 2 * 2 = 4 arrays.

Input: a = [2, 3, 1]
Output: 3

b[2] must be 1, so b[1] is 2 or 3. With b[1] = 2 only b[0] = 1 works, and with b[1] = 3 both 1 and 2 work for b[0]. The arrays are [1, 2, 1], [1, 3, 1] and [2, 3, 1].