We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Number of Ways to Reach a Position After Exactly k Steps

Difficulty: Medium


Problem Description

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 10^9 + 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.


Key Insights

  • To reach endPos from startPos in exactly k steps, the difference between the two positions must be achievable within k steps.
  • The difference diff = endPos - startPos must satisfy the conditions:
    • diff must be less than or equal to k.
    • The parity (even or odd) of diff must match the parity of k (both should be even or both should be odd).
  • The number of ways to reach the position can be computed using combinatorial mathematics, specifically binomial coefficients.

Space and Time Complexity

Time Complexity: O(k) for precomputing factorials and inverse factorials, O(1) for each query after precomputation. Space Complexity: O(k) for storing factorials and inverse factorials.


Solution

To solve the problem, we can use combinatorial mathematics. The main steps are:

  1. Compute the difference diff = endPos - startPos.
  2. Check if reaching endPos is feasible within k steps using the conditions mentioned in the Key Insights.
  3. Use the formula for combinations to calculate the number of ways to arrange the steps. Specifically, we will use the binomial coefficient C(n, r), which counts the number of ways to choose r steps in one direction (either left or right) out of n total steps.

We will utilize factorials and modular arithmetic to efficiently compute the binomial coefficients.


Code Solutions

def numWays(startPos, endPos, k):
    MOD = 10**9 + 7

    # Calculate the difference and check feasibility
    diff = endPos - startPos
    if abs(diff) > k or (k - diff) % 2 != 0:
        return 0

    # Calculate the number of steps in one direction
    steps_right = (k + diff) // 2
    steps_left = k - steps_right

    # Precompute factorials and inverse factorials
    fact = [1] * (k + 1)
    for i in range(2, k + 1):
        fact[i] = fact[i - 1] * i % MOD

    def mod_inv(x):
        return pow(x, MOD - 2, MOD)

    inv_fact = [1] * (k + 1)
    inv_fact[k] = mod_inv(fact[k])
    for i in range(k - 1, 0, -1):
        inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD

    # Calculate the number of combinations
    return fact[k] * inv_fact[steps_right] % MOD * inv_fact[steps_left] % MOD
← Back to All Questions