814. Beautiful Arrangement II — Constructible?

HardRecursionRecursion

Given n and k, determine if an arrangement of [1..n] exists with exactly k distinct absolute differences between consecutive elements. Return true if 1<=k<=n-1, false otherwise.

Input: Two integers n, k.

Output: Boolean.

Examples

Example 1
Input: 3, 2
Output: true
Explanation: [1,3,2] has diffs |2|,|1| -> 2 distinct.
Example 2
Input: 3, 0
Output: false
Explanation: k must be >= 1.
Example 3
Input: 1, 1
Output: false
Explanation: Single element has no consecutive diffs; k must be 0 but we need k>=1.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →