Number of Ways to Stay in the Same Place After Some Steps - Problem
You have a pointer at index 0 in an array of size arrLen. At each step, you can:
- Move 1 position to the left (if not at leftmost boundary)
- Move 1 position to the right (if not at rightmost boundary)
- Stay in the same place
Given two integers steps and arrLen, return the number of ways such that your pointer is still at index 0 after exactly steps steps.
Note: The pointer should not be placed outside the array at any time.
Since the answer may be too large, return it modulo 109 + 7.
Input & Output
Example 1 — Basic Case
$
Input:
steps = 3, arrLen = 2
›
Output:
4
💡 Note:
4 ways to return to position 0: Stay-Stay-Stay, Stay-Right-Left, Right-Left-Stay, Right-Left-Left (invalid), Left-Right-Stay (invalid). Valid ways: Stay-Stay-Stay, Stay-Right-Left, Right-Left-Stay, Right-Stay-Left = 4 ways
Example 2 — Single Position
$
Input:
steps = 2, arrLen = 1
›
Output:
1
💡 Note:
Only one position available, so the only way is Stay-Stay = 1 way
Example 3 — More Steps
$
Input:
steps = 4, arrLen = 3
›
Output:
8
💡 Note:
With 4 steps in a 3-position array, there are 8 different ways to end up back at position 0
Constraints
- 1 ≤ steps ≤ 500
- 1 ≤ arrLen ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array length and number of steps to take
2
Process
Count all valid move sequences
3
Output
Number of ways to return to start position
Key Takeaway
🎯 Key Insight: Use dynamic programming to count paths by building up from smaller subproblems
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code