Number of Ways to Stay in the Same Place

Hard

Description

Given an array of length arrLen, starting at index 0, you can move left, right, or stay. Return the number of ways to be at index 0 after exactly steps moves.

Examples

Input:steps = 3, arrLen = 2
Output:4
Explanation:

4 ways to return to 0.

Input:steps = 1, arrLen = 3
Output:1
Explanation:

With only 1 step available, there's only 1 way to return to index 0: stay at index 0 (don't move). Any other move would require at least 2 steps to return to the starting position.

Input:steps = 4, arrLen = 5
Output:14
Explanation:

With 4 steps and array length 5, there are multiple valid sequences that return to index 0 after exactly 4 moves.

Constraints

  • 1 ≤ steps ≤ 500

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!