Number of Ways to Reach a Position After Exactly k Steps - Problem

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

example_1.py — Basic Movement
$ Input: startPos = 1, endPos = 2, k = 3
Output: 3
💡 Note: There are 3 ways to reach position 2 from position 1 in exactly 3 steps: (1) Right → Left → Right, (2) Right → Right → Left, (3) Left → Right → Right. All three sequences end at position 2.
example_2.py — Same Position
$ Input: startPos = 2, endPos = 5, k = 10
Output: 210
💡 Note: Need to move distance 3 in 10 steps. This requires 6.5 right moves, but since we need integer moves, we need 6 right moves and 4 left moves (net movement = 6-4=2, but we need extra moves). Actually, we need 6.5 right moves, so (10+3)/2 = 6.5, which means 6 right and 4 left gives us net +2, but we need +3. Let me recalculate: we need (10+3)/2 = 6.5... Actually, (k+d) must be even. Here (10+3)=13 is odd, so impossible... Wait, let me recalculate properly.
example_3.py — Impossible Case
$ Input: startPos = 1, endPos = 2, k = 2
Output: 0
💡 Note: It's impossible to reach position 2 from position 1 in exactly 2 steps. We need odd number of net moves (1) but have even number of total steps (2), which makes it impossible since each step changes parity.

Visualization

Tap to expand
Brute ForceO(2^k)Try all pathsMemoizationO(k × range)Cache resultsCombinatoricsO(k)Math formulaoptimizeinsight2^kkComplexity ReductionKey Mathematical Insightr - l = distance (net movement)r + l = k (total steps)Therefore: r = (k + distance) / 2
Understanding the Visualization
1
Understand the Movement
We can move left (-1) or right (+1) on an infinite line
2
Count Required Moves
To travel distance d, we need r right moves and l left moves where r - l = d and r + l = k
3
Solve the System
From the equations: r = (k + d) / 2 and l = (k - d) / 2
4
Apply Combinatorics
The answer is C(k, r) - ways to choose positions for right moves
Key Takeaway
🎯 Key Insight: Transform a recursive pathfinding problem into a combinatorics question: 'In how many ways can we choose positions for right moves?'

Time & Space Complexity

Time Complexity
⏱️
O(k)

Computing combinations takes O(min(r, k-r)) where r is right_moves

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables for the calculation

n
2n
Linear Space

Constraints

  • 1 ≤ startPos, endPos ≤ 1000
  • 1 ≤ k ≤ 1000
  • Answer fits in a 32-bit signed integer after taking modulo 109 + 7
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.6K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen