Close
Topic list
Next Permutation
MEDIUM
15 mins
Greedy
Arrays
Topics (Covered in this problem)
Problem solved
Skill meter
Greedy
-
-
Arrays
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Next Permutation

Contributed by
Ashwani
Medium
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share

## Problem Statement

#### For example, [2, 1, 3, 4] is lexicographically smaller than [2, 1, 4, 3].

Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````2
3
1 2 3
5
2 3 1 4 5
``````
##### Sample Output 1:
``````1 3 2
2 3 1 5 4
``````
##### Explanation of sample input 1:
``````In the first test case, the lexicographically next greater permutation is [1, 3, 2].

In the second test case, the lexicographically next greater permutation is [2, 3, 1, 4, 5].
``````
##### Sample Input 2:
``````2
2
1 2
3
3 1 2
``````
##### Sample Output 2:
``````2 1
3 2 1
``````
##### Explanation for sample input 2:
``````In the first test case, the lexicographically next greater permutation is [2, 1].

In the second test case, the lexicographically next greater permutation is [3, 2, 1].
``````
Auto
Console