Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

11. Next Permutation

MediumDay 3 of 60Arrays & Hashing

Rearrange the integer array nums into the next arrangement that is larger in dictionary (lexicographic) order. If nums is already the largest arrangement, rearrange it into the smallest one (sorted ascending). Do it in place with O(1) extra memory.

Example 1
Input: nums = [1,2,3]
Output: [1,3,2]
Among the arrangements of 1, 2, 3, the one right after [1,2,3] is [1,3,2].
Example 2
Input: nums = [3,2,1]
Output: [1,2,3]
This is the largest arrangement, so we wrap around to the smallest.
Constraints
  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100
Hints
Frequently asked at: Google, Amazon, Adobe, Goldman Sachs, Flipkart
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min