> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Ways to Stay in the Same Place

> Tested Python solution for LeetCode 1269 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1269, [Hard](/catalog/hard). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1269   # by problem number
lcpy gen -s number_of_ways_to_stay_in_the_same_place_after_some_steps   # by problem name
```

## 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, 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.

### Examples

```
Input: steps = 3, arrLen = 2
Output: 4
Explanation: There are 4 differents ways to stay at index 0 after 3 steps.
- Right, Left, Stay
- Stay, Right, Left
- Right, Stay, Left
- Stay, Stay, Stay
```

```
Input: steps = 2, arrLen = 4
Output: 2
Explanation: There are 2 differents ways to stay at index 0 after 2 steps
- Right, Left
- Stay, Stay
```

```
Input: steps = 4, arrLen = 2
Output: 8
```

### Constraints

* 1 \<= steps \<= 500
* 1 \<= arrLen \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_ways_to_stay_in_the_same_place_after_some_steps/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_ways_to_stay_in_the_same_place_after_some_steps/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    def num_ways(self, steps: int, arr_len: int) -> int:
        mod = 1_000_000_007
        limit = min(steps // 2 + 1, arr_len)
        dp = [0] * limit
        dp[0] = 1
        for _ in range(steps):
            ndp = [0] * limit
            for i in range(limit):
                ndp[i] = dp[i]
                if i > 0:
                    ndp[i] += dp[i - 1]
                if i + 1 < limit:
                    ndp[i] += dp[i + 1]
                ndp[i] %= mod
            dp = ndp
        return dp[0]
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.