You are given three integers n, l, and r.
A ZigZag array of length n is defined as follows:
[l, r].Return the total number of valid ZigZag arrays.
Since the answer may be large, return it modulo 109 + 7.
A sequence is said to be strictly increasing if each element is strictly greater than its previous one (if exists).
A sequence is said to be strictly decreasing if each element is strictly smaller than its previous one (if exists).
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 <= 1091 <= l < r <= 75Problem Overview: You need to count how many arrays satisfy a zigzag pattern, where adjacent elements strictly alternate between increasing and decreasing. The task is combinatorial in nature, and brute-force generation quickly becomes infeasible as the array length grows.
Approach 1: Brute Force Enumeration (Exponential Time, O(n) Space)
The most direct idea is to generate every possible array and check whether it satisfies the zigzag condition. During generation, compare each adjacent pair and ensure the relationship alternates between < and >. This approach uses recursion or backtracking and keeps track of the previous value and the expected direction. While simple to reason about, the search space grows exponentially with the array length, leading to O(m^n) time complexity and O(n) recursion stack space.
Approach 2: Dynamic Programming with Direction State (O(n * m^2) Time, O(n * m) Space)
A better approach models the problem using dynamic programming. Let dp[i][v][d] represent the number of valid zigzag arrays of length i that end with value v, where d indicates whether the previous step was increasing or decreasing. To transition, iterate through all valid previous values that satisfy the required relation (smaller values for an increasing step, larger values for a decreasing step). This captures the alternating constraint explicitly, but each transition may scan many candidate values, producing O(n * m^2) time.
Approach 3: DP with Prefix Sum Optimization (O(n * m) Time, O(n * m) Space)
The optimized solution removes the inner scan using prefix sums. Instead of iterating over all smaller or larger values, maintain cumulative counts so each transition becomes a constant-time lookup. For example, when computing increasing states, you sum all counts from values smaller than the current value using a prefix array. For decreasing states, use a suffix or reversed prefix sum. This transforms the DP transition into O(1), reducing the total complexity to O(n * m) time and O(n * m) space. The approach combines ideas from math counting techniques and efficient DP transitions.
Recommended for interviews: Start by explaining the brute-force generation to show you understand the zigzag constraint. Then transition to the DP formulation that tracks the last value and direction. Interviewers typically expect the prefix-sum optimized DP because it demonstrates strong command of dynamic programming state design and transition optimization.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Backtracking | O(m^n) | O(n) | Useful for understanding the zigzag constraint or verifying small inputs |
| Dynamic Programming (State: index, value, direction) | O(n * m^2) | O(n * m) | Good intermediate solution when constraints are moderate |
| DP with Prefix Sum Optimization | O(n * m) | O(n * m) | Optimal approach for large inputs; eliminates expensive inner loops |
3700. Number of ZigZag Arrays II (Leetcode Hard) • Programming Live with Larry • 554 views views
Watch 1 more video solutions →Practice Number of ZigZag Arrays II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor