The smallest permutation
Practice
2.7 (74 votes)
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
94% Success 11574 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) of \(N\) elements which is a permutation. You are required to perform the following operation exactly one time:

  • Select two different indices \(i\) and \(j\) (\(1\le i<j\le n\)) and swap elements at these indices.

Your task is to find the lexicographically smallest array \(A\) you can achieve.

Input format

  • The first line contains an integer \(T\) denoting the number of test cases. 
  • The first line of each test case contains an integer \(N\) denoting the number of elements in array \(A\).
  • The second line of each test case contains \(N\) space-separated integers of array \(A\).

Output format

Print \(T\) lines. For each test case:

  • Print a single line containing \(N\) integers which is a lexicographically smallest permutation \(A\) after applying the operation exactly once.

Constraints

\(1 \leq T \leq 20000\)

\(2 \leq N \leq 200000\)

\(1 \leq A_i \leq N \forall i \in [1,N]\)

Sum of \(N\) over all test cases does not exceed 200000

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:10
49 votes
Tags:
Ad-HocVery-EasySimple-math
Points:20
21 votes
Tags:
ApprovedBasic ProgrammingEasyGreedy AlgorithmsOpen
Points:20
34 votes
Tags:
AlgorithmsEasyGreedy Algorithms