Next Permutation Task

Design a function to generate the lexicographically next greater permutation of a given sequence of integers that form a permutation.

A permutation contains all integers from 1 to N exactly once. If no such permutation exists, return the lexicographically smallest permutation.

Example:

Input:
T = 1
N = 4
permutation = [2, 1, 3, 4]
Output:
[2, 1, 4, 3]
Explanation:

The permutation [2, 1, 3, 4] is lexicographically smaller than [2, 1, 4, 3]. Hence the next permutation is [2, 1, 4, 3].

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 10000
  • 1 <= P[i] <= N
Note:
You do not need to print the output; the necessary lines have been included.
Be the first one to answer
Add answer anonymously...
UST Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter