- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to check number of ways we can move k times and return back to first place in python

Suppose we are at position 0 of n length list, and on each step, we can move right one place or left one place (not exceeding bounds), or stay at the same position. Now if we can take exactly k steps, then we have to find how many unique walks we can take and reach back to index 0. If the answer is very large return it mod 10^9 + 7.

So, if the input is like n = 7 k = 4, then the output will be 9, as the actions are-

- [Right, Right, Left, Left],
- [Right, Left, Right, Left],
- [Stay, Stay, Stay, Stay],
- [Right, Left, Stay, Stay],
- [Stay, Stay, Right, Left],
- [Right, Stay, Stay, Left],
- [Right, Stay, Left, Stay],
- [Stay, Right, Left, Stay],
- [Stay, Right, Stay, Left],

To solve this, we will follow these steps −

- m := 10^9 + 7
- N := length
- Define a function dp() . This will take i, jumps
- if jumps is same as 0, then
- return (true when i is same as 0, otherwise false)

- count := dp(i, jumps - 1)
- if i >= 0, then
- count := count + dp(i + 1, jumps - 1)

- if i <= N - 1, then
- count := count + dp(i - 1, jumps - 1)

- return count
- From the main method do the following:
- return dp(0, n) mod m

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, length, n): MOD = 10 ** 9 + 7 N = length def dp(i, jumps): if jumps == 0: return +(i == 0) count = dp(i, jumps - 1) if i >= 0: count += dp(i + 1, jumps - 1) if i <= N - 1: count += dp(i - 1, jumps - 1) return count return dp(0, n) % MOD ob = Solution() n = 7 k = 4 print(ob.solve(n, k))

## Input

7, 4

## Output

9

- Related Questions & Answers
- Program to count number of ways we can place nonoverlapping edges to connect all nodes in C++
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to find number of ways we can decode a message in Python
- Program to count number of ways we can throw n dices in Python
- Program to find number of ways we can split a palindrome in python
- Program to count number of ways we can distribute coins to workers in Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to check we can reach end of list by starting from k in Python
- Program to check how many ways we can choose empty cells of a matrix in python
- Number of Ways to Paint N × 3 Grid in C++ program
- In-place Move Zeros to End of List in Python
- Program to count how many ways we can cut the matrix into k pieces in python
- Program to find number of ways we can reach to the next floor using stairs in Python

Advertisements