Number of Ways to Reach a Position After Exactly K Steps
Problem statement
You stand at integer position startPos. Each step moves exactly one unit left or right.
Return the number of distinct step sequences that reach endPos after exactly k steps, modulo 1,000,000,007.
Function
numberOfWays(startPos: int, endPos: int, k: int) → intExamples
Example 1
startPos = 1endPos = 2k = 3return = 3Exactly two right moves and one left move are needed, which can be ordered in three ways.
Example 2
startPos = 2endPos = 5k = 10return = 0The distance and step count have different parity.
Constraints
-1000 <= startPos, endPos <= 1000.0 <= k <= 1000.