Arrangements Without Steep Descents
InterviewTime limit1sMemory limit1024 MB
Print every ordered selection of k distinct numbers from 1 to n where no adjacent pair drops by more than 1, sorted lexicographically.
- Level
Medium4 of 10
- Topics
- Backtracking, Combinatorics
- Solved
- No attempts yet
Problem
An arrangement of taken at a time is an array of distinct natural numbers, each between and .
A pair of adjacent elements is a descent if . A descent is steep if .
Given and , print all arrangements of taken at a time that have no steep descents. Sort the arrangements by the first element, then by the second element when the first is equal, then by the third, and so on.
Input
The first line contains a natural number . The second line contains a natural number ().
Output
Print all arrangements of taken at a time without steep descents, one per line. Separate the numbers within an arrangement with spaces.