Imagine you're standing on an infinite number line at position startPos. You can move one step left or one step right at a time. Your goal is to reach position endPos in exactly k steps.
The challenge? Count all the different ways to make this journey. Two paths are considered different if the sequence of steps (left/right moves) is different, even if they visit the same intermediate positions.
Example: To go from position 1 to position 2 in exactly 3 steps, you could go: Right→Left→Right or Left→Right→Right. These are two different ways!
Since the answer can be very large, return it modulo 109 + 7.
Input & Output
Visualization
Time & Space Complexity
Computing combinations takes O(min(r, k-r)) where r is right_moves
Only using a few variables for the calculation
Constraints
- 1 ≤ startPos, endPos ≤ 1000
- 1 ≤ k ≤ 1000
- Answer fits in a 32-bit signed integer after taking modulo 109 + 7