Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew 🚀
Contest SolutionsPractice
Practice Sheets
MasterclassesLive 👨🏻‍💻

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
© 2026coding75•crackDSA™•Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021•Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/CodeChef/Starters 207/Maximum Smaller
PrevNext
CodeChefCodeChef
4 Star
Starters 207

Maximum Smaller

SortingArrayHashing
Loading...
Solve on CodeChef

Problem Statement

Official algorithmic problem description and constraints.

Open on CodeChef

You are given an array A


A of size N


N.


The score of a permutation P


P of the integers [1,N]


[1,N] is defined as follows:

  • If Pi=i

  • Pi
  • ​=i for at least one i

  • i (1≤i≤N

  • 1≤i≤N), the score is −1

  • −1.
  • Otherwise, the score is the number of indices i

  • i (1≤i≤N

  • 1≤i≤N) such that APi≤Ai

  • APi
  • ​
  • ​≤Ai
  • ​.

Find the maximum possible score for some permutation P


P. You also need to print a valid permutation.

Input Format

  • The first line of input will contain a single integer T

  • T, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
  • The first line of each test case contains a single integer N

  • N - the size of the array.
  • The second line contains N

  • N integers - A1,A2,…,AN

  • A1
  • ​,A2
  • ​,…,AN
  • ​.

Output Format

For each test case, output the following:

  • On a new line, first output the maximum possible score.
  • Next, output N

  • N integers, P1,P2,…,PN

  • P1
  • ​,P2
  • ​,…,PN
  • ​, any valid permutation that obtains the maximum score.

If multiple answers are possible, all will be accepted.

Constraints

  • 1≤T≤104

  • 1≤T≤104
  • 2≤N≤2⋅105

  • 2≤N≤2⋅105
  • 1≤Ai≤N

  • 1≤Ai
  • ​≤N
  • The sum of N

  • N over all test cases does not exceed 2⋅105

  • 2⋅105
  • .

Sample 1:

Input


Output


3 3 1 1 1 3 3 1 2 2 2 2
3 2 3 1 2 3 1 2 2 2 1

Explanation:

Test Case 1 : Any permutation P


P which does not have Pi=i


Pi

​=i for any index i


i, would have a score of 3


3 because all values are 1


1, so APi≤Ai


APi

​

​≤Ai

​ always holds.

Test Case 2 : P=[3,1,2]


P=[3,1,2] means that index i=1,3


i=1,3 are good because AP1=A3=2≤A1


AP1

​

​=A3

​=2≤A1

​, and AP3=A2=1≤A3


AP3

​

​=A2

​=1≤A3

​. It can be shown that a larger score is impossible.

Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Starters 207
Previous ProblemNext Problem