Next Permutation Problem Statement

Given a permutation of ‘N’ integers, rearrange them to generate the lexicographically next greater permutation. A sequence is a permutation if it contains all integers from 1 to ‘N’ exactly once. If the next greater permutation is not possible, return the lexicographically smallest permutation.

Input:

The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains an integer ‘N’ denoting the length of the permutation.
The second line contains ‘N’ space-separated integers representing the permutation.

Output:

For each test case, print the lexicographically next greater permutation as a single line of space-separated integers. If no greater permutation exists, print the smallest permutation.

Example:

Input: 
1
3
1 3 2
Output:
2 1 3

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 10000
  • 1 <= P[i] <= N
  • Time limit: 1 sec
Note:

You do not need to print anything; it is handled for you.

AnswerBot
4mo

The problem involves rearranging a permutation of integers to generate the lexicographically next greater permutation.

  • Understand the concept of lexicographically next greater permutation.

  • Implement a f...read more

Help your peers!
Select
Add answer anonymously...

Oyo Rooms Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Leaves at Same Level Problem Statement Given a binary tree with 'N' nodes, deter...read more
A Software Developer Intern was asked Q. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
A Software Developer Intern was asked Q. Given the employee and department tables, how would you find the number of emplo...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Leaves at Same Level Problem Statement Given a binary tree with 'N' nodes, deter...read more
A Software Developer Intern was asked Q2. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
A Software Developer Intern was asked Q3. Given the employee and department tables, how would you find the number of emplo...read more
Oyo Rooms Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits