Next permutation and rearrangement
Rearranging means putting values into new positions by a rule. Next permutation finds the very next arrangement in dictionary order by changing the shortest possible suffix. Sign rearrangement puts positives and negatives into alternating slots, either with a spare array or, in place, by rotating a value back to the first slot that needs it.
How to recognise it
- You must find the next (or previous) arrangement of the same values in dictionary order.
- A number must become the next larger one using the same digits.
- Values must alternate by a property (sign, odd or even) while keeping their original order.
- The rule says in place or O(1) extra memory, so a second array is not allowed.
The idea in one picture
Write all arrangements of 1, 2, 3 in dictionary order: 123, 132, 213, 231, 312, 321. To step from one to the next, change as little as possible, as far to the right as possible, just like adding 1 to a number changes the last digits first.
Next permutation in three steps
| Step | What | Why |
|---|---|---|
| 1 | Find the first index i, from the right, with nums[i] < nums[i + 1] | Everything after it is falling and cannot grow |
| 2 | Swap nums[i] with the smallest value on its right that is bigger | Grow index i by the least amount |
| 3 | Reverse everything after i | Make the suffix as small as possible |
If step 1 finds no index, the whole array is falling. Reverse all of it to get the smallest arrangement.
i = n - 2
while i >= 0 and nums[i] >= nums[i + 1]: i = i - 1
if i >= 0:
j = n - 1
while nums[j] <= nums[i]: j = j - 1
swap nums[i], nums[j]
reverse nums from i + 1 to n - 1
A guided example: [1, 2, 7, 4, 3, 1]
| step | array | note |
|---|---|---|
| 1 | [1, 2, 7, 4, 3, 1] | from the right, 1 3 4 7 rise; 2 is smaller than 7, so i = 1 |
| 2 | [1, 3, 7, 4, 2, 1] | the smallest value bigger than 2 on the right is 3; swap them |
| 3 | [1, 3, 1, 2, 4, 7] | reverse after index 1 |
Rearranging by sign
With a spare array, keep one write index for each kind, and move it by 2 after each write. In place, walk the array and remember the first slot holding the wrong sign. When a value of the needed sign appears, rotate it back into that slot. The values in between shift right by one and keep their order. That is O(1) memory but up to O(n²) time.
Common mistakes
- Using
>instead of>=in step 1 or<instead of<=in step 2, which breaks arrays with repeated values. - Sorting the suffix instead of reversing it. The answer is the same, but it costs O(n log n).
- Forgetting the case with no drop, so the code reads
nums[-1]or leaves the array unchanged. - Swapping a value straight into its slot in the in-place sign problem. A swap breaks the original order of the others.
Problems
- Rearrange Array Elements by Signmediumguide ready
- Next Permutationmediumguide ready
- Rearrange the array in alternating positive and negative items with O(1) extra spacemediumguide ready
- Find next greater number with same set of digitsmediumguide ready