Number of Ways to Rearrange Sticks With K Sticks Visible

There are n uniquely-sized sticks whose lengths are integers from 1 to n. You want to arrange the sticks such that exactly k sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it.

Given n and k, return the number of such arrangements. Since the answer may be large, return it modulo 10^9 + 7.

Example 1
Inputn = 3, k = 2
Output3
[1,3,2], [2,3,1], and [2,1,3] are the only arrangements such that exactly 2 sticks are visible.
Example 2
Inputn = 5, k = 5
Output1
[1,2,3,4,5] is the only arrangement such that all 5 sticks are visible.

Constraints

  • 1 <= n <= 1000
  • 1 <= k <= n

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