Tap here to switch tabs
Problem
Submissions

Problem: Beautiful Arrangement II

med
30 min
Try to solve the Beautiful Arrangement II problem.

Statement

Given two integers n and k, construct a list ans containing nn integers such that:

  • ans is a permutation of all integers from 11 to nn.

  • The list of adjacent absolute differences [∣‘ans‘[0]−‘ans‘[1]∣,∣‘ans‘[1]−‘ans‘[2]∣,…,∣‘ans‘[n−2]−‘ans‘[n−1]∣][|`ans`[0] - `ans`[1]|, |`ans`[1] - `ans`[2]|, \dots, |`ans`[n - 2] - `ans`[n - 1]|] contains exactly kk distinct values.

Return the list ans. If there are multiple valid answers, return any of them.

Constraints:

  • 1≤‘k‘<‘n‘≤1041 \leq `k` < `n` \leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Beautiful Arrangement II

med
30 min
Try to solve the Beautiful Arrangement II problem.

Statement

Given two integers n and k, construct a list ans containing nn integers such that:

  • ans is a permutation of all integers from 11 to nn.

  • The list of adjacent absolute differences [∣‘ans‘[0]−‘ans‘[1]∣,∣‘ans‘[1]−‘ans‘[2]∣,…,∣‘ans‘[n−2]−‘ans‘[n−1]∣][|`ans`[0] - `ans`[1]|, |`ans`[1] - `ans`[2]|, \dots, |`ans`[n - 2] - `ans`[n - 1]|] contains exactly kk distinct values.

Return the list ans. If there are multiple valid answers, return any of them.

Constraints:

  • 1≤‘k‘<‘n‘≤1041 \leq `k` < `n` \leq 10^4