DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

Next Permutation

**Problem Statement:** A permutation of an array of integers is an arrangement of its members into a sequence or linear order. Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. **Hint / Expected Approach:** Find rightmost ascent + swap + reverse suffix **Edge Cases to Consider:** - (1) Descending array (last permutation) - (2) Single element - (3) Two elements

Examples

Example 1
Input: [1,2,3]
Output: [1,3,2]
Example 2
Input: [3,2,1]
Output: [1,2,3]
Example 3
Input: [1,1,5]
Output: [1,5,1]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪Descending array (last permutation)
  • ▪Single element
  • ▪Two elements
Frequently Asked At
GoogleAmazonMicrosoft