Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution

Problem – Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution

You are given two positive integers startPos and endPos. Initially, you are standing at position startPos on an infinite number line. With one step, you can move either one position to the left, or one position to the right.

Given a positive integer k, return the number of different ways to reach the position endPos starting from startPos, such that you perform exactly k steps. Since the answer may be very large, return it modulo 109 + 7.

Two ways are considered different if the order of the steps made is not exactly the same.

Note that the number line includes negative integers.

Example 1:

Input: startPos = 1, endPos = 2, k = 3
Output: 3
Explanation: We can reach position 2 from 1 in exactly 3 steps in three ways:
- 1 -> 2 -> 3 -> 2.
- 1 -> 2 -> 1 -> 2.
- 1 -> 0 -> 1 -> 2.
It can be proven that no other way is possible, so we return 3.

Example 2:

Input: startPos = 2, endPos = 5, k = 10
Output: 0
Explanation: It is impossible to reach position 5 from position 2 in exactly 10 steps.

Constraints:

  • 1 <= startPos, endPos, k <= 1000

Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution in Java

    int p = 1000000007;
    public int numberOfWays(int a, int b, int k) {
        if ((a - b - k) % 2 != 0) return 0;
        if (Math.abs(a - b) > k) return 0;
        long res = 1L;
        for (int i = 0; i < (b - a + k) / 2; ++i) {
            res = res * (k - i) % p;
            res = res * inv(i + 1) % p;
        }
        return (int)res;
    }

    private long inv(long a) {
        if (a == 1) return 1;
        return (p - p / a) * inv(p % a) % p;
    }

Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution in C++

    int p = 1e9 + 7;
    int numberOfWays(int a, int b, int k) {
        if ((a - b - k) % 2) return 0;
        if (abs(a - b) > k) return 0;
        long long res = 1L;
        for (int i = 0; i < (b - a + k) / 2; ++i) {
            res = res * (k - i) % p;
            res = res * inv(i + 1) % p;
        }
        return res;
    }

    long inv(long a) {
        if (a == 1) return 1;
        return (p - p / a) * inv(p % a) % p;
    }

Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution in Python

    def numberOfWays(self, a, b, k):
        if (a - b - k) % 2: return 0
        return comb(k, (b - a + k) // 2) % (10 ** 9 + 7)
Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution Review:

In our experience, we suggest you solve this Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution

Find on Leetcode

Conclusion:

I hope this Number of Ways to Reach a Position After Exactly k Steps LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *