FastPrepNext Permutation

Next Permutation

Amazon logoAmazon● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

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

See Amazon hiring insights
Saved
public int[] nextPermutation(int[] nums) {
  // Write your code here.
}
nums[1,2,3]
expected[1,3,2]
Checking account…