Problem Description
Given two positive integers startPos and endPos, you start at position startPos on an infinite number line. In each step you can move one unit left or right. Given a positive integer k, determine the number of different ways (order matters) to reach endPos in exactly k steps. If the number exceeds 10^9+7, return it modulo 10^9+7.
Key Insights
- The net displacement must be exactly |endPos - startPos|, and any extra moves must cancel out.
- If d is the distance (|endPos - startPos|), then you need (k - d) extra moves that come in canceling pairs. Therefore, (k - d) must be even.
- Let r be the number of right moves. Then, from the equations:
- r + l = k and r - l = (endPos - startPos), we extract r = (k + (endPos - startPos)) / 2.
- The answer is given by the binomial coefficient C(k, r) modulo 10^9+7.
Space and Time Complexity
Time Complexity: O(k) for precomputing factorials and modular inverses.
Space Complexity: O(k) for storing factorials and inverse factorials.
Solution
The solution involves combinatorics. First, check for feasibility: if the absolute distance d between startPos and endPos is greater than k or if (k - d) is odd, no solution exists. Otherwise, compute the exact number of steps to the right using r = (k + (endPos - startPos)) / 2. The total number of valid ways is then given by the binomial coefficient C(k, r) (i.e., choosing r steps to be right out of k steps). To compute C(k, r) modulo 10^9+7, we precompute factorials and their modular inverses using Fermat's Little Theorem.
Code Solutions
Below are the implementations in Python, JavaScript, C++, and Java with inline comments.