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

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 109 + 7.

Two ways are considered different if the order of the steps made is not exactly the same.

Note: The number line includes negative integers.

Input & Output

Example 1 — Basic Movement
$ Input: startPos = 1, endPos = 2, k = 3
Output: 3
💡 Note: 3 ways to reach position 2 from position 1 in exactly 3 steps: RLL (1→2→1→0→1→2), LRL (1→0→1→2), LLR (1→0→-1→0→1→2). Wait, let me recalculate: RLL: 1→2→1→0, LRL: 1→0→1→2, LLR: 1→0→-1→0. Actually the 3 ways are: RLR: 1→2→1→2, LRR: 1→0→1→2, RRL: this doesn't work. Let me recalculate properly: We need exactly 3 steps to go from 1 to 2 (distance 1). Ways: RLR (1→2→1→2), LRR (1→0→1→2), RLL then R won't work in 3 steps. The correct 3 ways are: RLR, LRR, and RRL doesn't work. Actually: RLR, LRR, LRL all reach position 2 in 3 steps.
Example 2 — Same Position
$ Input: startPos = 2, endPos = 2, k = 2
Output: 2
💡 Note: 2 ways to stay at position 2 in exactly 2 steps: RL (2→3→2) or LR (2→1→2). Both sequences return to the starting position.
Example 3 — Impossible Case
$ Input: startPos = 1, endPos = 3, k = 1
Output: 0
💡 Note: Impossible to reach position 3 from position 1 in exactly 1 step, since the distance is 2 but we can only move 1 step total.

Constraints

  • 1 ≤ startPos, endPos ≤ 1000
  • 1 ≤ k ≤ 1000

Visualization

Tap to expand
Number of Ways to Reach Position INPUT 1 start 2 end 0 3 4 Move left(-1) or right(+1) -1 +1 startPos = 1 endPos = 2 k = 3 (steps) dist = |2-1| = 1 ALGORITHM (DP) 1 Define State dp[i][j] = ways to reach position j in i steps 2 Recurrence dp[i][j] = dp[i-1][j-1] + dp[i-1][j+1] 3 Base Case dp[0][startPos] = 1 DP Table (simplified) step pos0 pos1 pos2 pos3 0 0 1 0 0 1 1 0 1 0 2 0 2 0 1 3 2 0 3 0 4 Answer dp[k][endPos] = dp[3][2] FINAL RESULT 3 Valid Paths: Path 1: R R L 1 --> 2 --> 3 --> 2 Path 2: R L R 1 --> 2 --> 1 --> 2 Path 3: L R R 1 --> 0 --> 1 --> 2 Output: 3 OK - Verified mod 10^9+7 Key Insight: This is a combinatorics problem solvable with DP. Need exactly R right steps and L left steps where: R + L = k (total steps) and R - L = dist (net displacement). Solution exists only if (k - dist) is even. For k=3, dist=1: R=2, L=1. Answer = C(3,2) = 3 ways to arrange 2 rights and 1 left in 3 positions. TutorialsPoint - Number of Ways to Reach a Position After Exactly k Steps | DP Approach Time: O(k * range) | Space: O(k * range) where range = k + |endPos - startPos|
Asked in
Google 15 Amazon 12 Microsoft 8
23.5K Views
Medium Frequency
~25 min Avg. Time
890 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