Imagine you have a pointer starting at position 0 in an array of size arrLen. At each step, you can make one of three moves:
- Move left (decrease position by 1)
- Move right (increase position by 1)
- Stay in the same position
Important: The pointer must never go outside the array bounds (positions 0 to arrLen-1).
Given steps total moves and array length arrLen, find the number of ways to end up back at position 0 after exactly steps moves.
Since the answer can be very large, return it modulo 109 + 7.
Example: With steps = 3 and arrLen = 2, you could go: stay→stay→stay, or right→left→stay, etc.
Input & Output
Visualization
Time & Space Complexity
We iterate through each step and for each step, we process positions up to min(steps, arrLen) since we can't go further than steps distance from origin
We only store current and previous DP arrays, each of size min(steps, arrLen)
Constraints
- 1 ≤ steps ≤ 500
- 1 ≤ arrLen ≤ 106
- Answer fits in 32-bit integer after modulo operation