Number of Ways to Stay in the Same Place After Some Steps

You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place. The pointer should not be placed outside the array at any time.

Given two integers steps and arrLen, return the number of ways such that your pointer is still at index 0 after exactly steps steps. Since the answer may be too large, return it modulo 10^9 + 7.

Example 1
Inputsteps = 3, arrLen = 2
Output4
There are 4 different ways to stay at index 0 after 3 steps: Right then Left then Stay, Stay then Right then Left, Right then Stay then Left, and Stay then Stay then Stay.
Example 2
Inputsteps = 2, arrLen = 4
Output2
There are 2 different ways to stay at index 0 after 2 steps: Right then Left, and Stay then Stay.

Constraints

  • 1 <= steps <= 500
  • 1 <= arrLen <= 10^6

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate