All problems
0942MediumMathCombinatorics

Placing k Markers With None Adjacent

Tracked in this browser only
Write code

Trains the technique from

LeetCode 4002Count Valid Sequences

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A strip has n slots in a row, and k markers are to be placed in them, at most one per slot.

No two markers may sit in neighbouring slots.

Return how many arrangements there are, modulo 10^9 + 7.

Examples

Example 1

Input
n = 9, k = 3
Output
35

Three markers over nine slots with none touching leaves seven slots to choose three positions from, which is 35 arrangements.

Example 2

Input
n = 3, k = 2
Output
1

The only arrangement puts markers in the first and last slots.

Example 3

Input
n = 2, k = 2
Output
0

Two markers cannot both fit in two neighbouring slots, so there is no arrangement.

Constraints

  • 1 <= n <= 5 * 10^5
  • 1 <= k <= n

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def count_valid_sequences(n: int, k: int) -> int:
Java
public int countValidSequences(int n, int k)
September 7
Apply