Skip to the code
cf.
+25 XP

#299 Next Layout Arrangement

medium

A layout tool lets designers flip through every arrangement of the widgets on a page, in sorted (dictionary) order. An arrangement is an array of widget numbers in order.

Write nextArrangement(order) that returns a new array: the arrangement that comes right after order in sorted order. If order is already the last arrangement (sorted descending), wrap around and return the first one (sorted ascending).

Numbers may repeat. Arrangements that look the same count once.

Example 1

Input: order = [1,2,3]
Output: [1,3,2]

In sorted order, [1, 2, 3] is followed by [1, 3, 2].

Example 2

Input: order = [3,2,1]
Output: [1,2,3]

[3, 2, 1] is the last arrangement, so wrap around to the first.

Constraints

  • 1 <= order.length <= 100
  • 0 <= order[i] <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).