Number of Ways to Reach a Position After Exactly k Steps

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.

Example 1
InputstartPos = 1, endPos = 2, k = 3
Output3
We can reach position 2 from 1 in exactly 3 steps in three ways, and no other way is possible.
Example 2
InputstartPos = 2, endPos = 5, k = 10
Output0
It is impossible to reach position 5 from position 2 in exactly 10 steps.

Constraints

  • 1 <= startPos, endPos, k <= 1000

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate