LeetCode Problem

How to Solve Next Permutation

The problem asks to generate the next lexicographically greater permutation of an array. By identifying the largest decreasing suffix, the next permutation is found by swapping an element with the smallest element larger than it. This process follows two-pointer scanning and invariant tracking principles, ensuring an optimal solution in linear time.

GhostInterview Help

Need help with Next Permutation without spending extra time grinding it?

GhostInterview can read Next Permutation from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #31Two-pointer scanning with invariant trackingReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Two-pointer scanning with invariant tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The problem asks to generate the next lexicographically greater permutation of an array. By identifying the largest decreasing suffix, the next permutation is found by swapping an element with the smallest element larger than it. This process follows two-pointer scanning and invariant tracking principles, ensuring an optimal solution in linear time.

Problem Statement

Given an array of integers, find its next permutation in lexicographical order. If the current permutation is the largest possible, return the smallest permutation. A permutation is an arrangement of numbers in a sequence, and the next permutation is the one that comes after it in sorted order. If no larger permutation exists, return the array sorted in ascending order.

To solve this, we need to identify the largest suffix that is in decreasing order. Once identified, we swap the rightmost element of this suffix with the smallest element from the suffix that is larger than it, and then reverse the suffix. This approach guarantees that the next permutation is generated with minimal changes.

Examples

Example 1

Input: nums = [1,2,3]

Output: [1,3,2]

Example details omitted.

Example 2

Input: nums = [3,2,1]

Output: [1,2,3]

Example details omitted.

Example 3

Input: nums = [1,1,5]

Output: [1,5,1]

Example details omitted.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100

Solution Approach

Identifying the Decreasing Suffix

Scan the array from right to left to find the largest suffix that is in decreasing order. This identifies the point where the permutation stops being in increasing order.

Finding the Swap Candidate

Find the smallest number in the suffix that is greater than the element before the decreasing suffix. This will be the element to swap to get the next greater permutation.

Reversing the Suffix

Once the swap is done, reverse the suffix to ensure the next permutation is the smallest possible arrangement after the swap.

Complexity Analysis

MetricValue
TimeO(n)
SpaceO(1)

The solution runs in O(n) time because we scan the array once to identify the suffix, perform a swap, and reverse the suffix. The space complexity is O(1) as no additional space is required beyond the input array.

What Interviewers Usually Probe

  • Checks for efficient in-place manipulation of the array.
  • Assesses understanding of permutations and their properties.
  • Tests ability to apply two-pointer techniques and invariant tracking in array problems.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to reverse the suffix after swapping.
  • Not correctly identifying the largest decreasing suffix.
  • Misunderstanding the termination condition when no next permutation is possible.

Follow-up variants

  • Generate the previous permutation instead of the next.
  • Handle larger arrays with more elements.
  • Optimize the solution for specific types of input arrays.

How GhostInterview Helps

  • The GhostInterview solver guides you through the process of identifying the key operations in the next permutation algorithm.
  • It provides detailed explanations of the two-pointer scanning technique and invariant tracking to ensure efficient execution.
  • The solver's step-by-step guidance helps you avoid common mistakes and master the critical elements of this problem.

Topic Pages

FAQ

What is the main pattern used in the Next Permutation problem?

The main pattern involves two-pointer scanning with invariant tracking to efficiently generate the next permutation.

How can I ensure that the next permutation is generated correctly?

You must identify the largest decreasing suffix, swap elements appropriately, and reverse the suffix to obtain the next permutation.

What is the time complexity of the Next Permutation algorithm?

The time complexity is O(n) because we scan the array and perform constant-time operations (swap and reverse).

Is the Next Permutation problem solvable in constant space?

Yes, the solution uses O(1) extra space, as the operations are done in-place on the input array.

What is the failure mode of the Next Permutation problem?

The failure mode occurs when the array is in descending order, and the next permutation is the smallest, requiring a complete rearrangement of the array.

GhostInterview Solver

Need direct help with Next Permutation instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Next Permutation from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.