Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Next Permutation

Medium

Topics
ArrayTwo Pointers

Rearrange nums into the lexicographically next greater permutation. If no greater permutation exists, rearrange to the lowest order (sorted ascending). Return the resulting array. Do it in-place with O(1) extra memory.

Example 1

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

Example 2

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

Example 3

Input:  nums = [1,1,5]
Output: [1,5,1]

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access