FastPrepNext Permutation
Problem · Array

Next Permutation

Learn this problem
MediumAmazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Return the lexicographically next permutation of nums. If the current ordering is the greatest possible, return the smallest ordering. Duplicate values are allowed.

Function

nextPermutation(nums: int[]) → int[]

Examples

Example 1

nums = [1,2,3]return = [1,3,2]

Swapping the final two values gives the next greater ordering.

Example 2

nums = [-1,0,-1]return = [0,-1,-1]

Covers wraparound, duplicates, pivot placement, tiny arrays, equality, and signed values.

Example 3

nums = [2,3,1,3,3]return = [2,3,3,1,3]

Covers wraparound, duplicates, pivot placement, tiny arrays, equality, and signed values.

Constraints

  • 1 <= nums.length <= 10000
  • Values fit in a signed 32-bit integer.

More Amazon problems

drafts saved locally
public int[] nextPermutation(int[] nums) {
  // Write your code here.
}
nums[1,2,3]
expected[1,3,2]
checking account