Number of ZigZag Arrays III Problem
Description
LeetCode Problem 3916.
You are given three integers n, l, and r.
A ZigZag array of length n is defined as follows:
- Each element lies in the range
[l, r]. - No two adjacent elements are equal.
- No three consecutive elements form a strictly increasing or strictly decreasing sequence.
Return the total number of valid ZigZag arrays.
Since the answer may be large, return it modulo 10^9 + 7.
Example 1:
Input: n = 3, l = 4, r = 5
Output: 2
Explanation:
There are only 2 valid ZigZag arrays of length n = 3 using values in the range [4, 5]:
- [4, 5, 4]
- [5, 4, 5]
Example 2:
Input: n = 3, l = 1, r = 3
Output: 10
Explanation:
There are 10 valid ZigZag arrays of length n = 3 using values in the range [1, 3]:
- [1, 2, 1], [1, 3, 1], [1, 3, 2]
- [2, 1, 2], [2, 1, 3], [2, 3, 1], [2, 3, 2]
- [3, 1, 2], [3, 1, 3], [3, 2, 3]
All arrays meet the ZigZag conditions.
Constraints:
3 <= n <= 2001 <= l < r <= 10^9
Solution
Approach: Counting by Polynomial Interpolation
The values only matter through their relative order, so the number of ZigZag arrays depends only on m = r - l + 1. Since every adjacent pair must differ and no three elements may be monotone, the array alternates: it goes up, down, up, ... or down, up, down, .... By symmetry (replace x with l + r - x) both patterns have the same count, so the answer is 2 * U(m), where U(m) counts arrays that start by going up.
For a small m, U(m) is a simple DP over the last value. Keep two arrays: a[v] counts arrays ending at value v whose next step must go up, and b[v] counts those whose next step must go down. Adding one element turns a into b through a prefix sum of smaller values, and b into a through a suffix sum of larger values. Start with a[v] = 1 for every v, and U(m) is the total of a and b after n elements.
But m can be as large as 10^9. The number of such arrays is a polynomial in m of degree at most n, so compute U(0), U(1), ..., U(n) with the DP and evaluate the polynomial at m with Lagrange interpolation modulo 10^9 + 7. If m <= n, the DP value is used directly.
Complexity: time O(n^3) for the DP on n + 1 small sizes (plus O(n log MOD) for the interpolation), and space O(n).
class Solution:
def zigZagArrays(self, n: int, l: int, r: int) -> int:
mod = 10**9 + 7
m = r - l + 1
def count(m: int) -> int:
# sequences of length n over m values that go up, down, up, ...
a = [1] * m # next step must go up
b = [0] * m # next step must go down
for _ in range(n - 1):
na = [0] * m
nb = [0] * m
s = 0
for w in range(m):
nb[w] = s
s = (s + a[w]) % mod
s = 0
for w in range(m - 1, -1, -1):
na[w] = s
s = (s + b[w]) % mod
a, b = na, nb
return (sum(a) + sum(b)) % mod
# the count is a polynomial of degree n in m: compute n + 1 points, then interpolate
ys = [count(i) for i in range(n + 1)]
if m <= n:
return 2 * ys[m] % mod
pre = [1] * (n + 2)
for i in range(n + 1):
pre[i + 1] = pre[i] * (m - i) % mod
suf = [1] * (n + 2)
for i in range(n, -1, -1):
suf[i] = suf[i + 1] * (m - i) % mod
fact = [1] * (n + 1)
for i in range(1, n + 1):
fact[i] = fact[i - 1] * i % mod
ans = 0
for i in range(n + 1):
term = ys[i] * pre[i] % mod * suf[i + 1] % mod
term = term * pow(fact[i] * fact[n - i] % mod, mod - 2, mod) % mod
if (n - i) % 2:
term = -term
ans += term
return 2 * ans % mod