Skip to content

Practice Number of Ways to Reach a Position After Exactly k Steps

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2400
LeetCode Problem

Number of Ways to Reach a Position After Exactly k Steps

Problem Statement

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 10^9 + 7. Two ways are considered different if the order of the steps made is not exactly the same. Note that the number line includes negative integers.

Source: Number of Ways to Reach a Position After Exactly k Steps on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
startPos = 1, endPos = 2, k = 3
Output
3

# Example 2

Input
startPos = 2, endPos = 5, k = 10
Output
0

Constraints

1 <= startPos, endPos, k <= 1000

Related LeetCode Problems

Practice more problems similar to Number of Ways to Reach a Position After Exactly k Steps.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.