3rd Smallest and Largest
Practice
4.4 (7 votes)
Easy
Problem
50% Success 2252 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Alice decides to challenge Bob to a problem. She gives him a number.

Bob can rearrange the digits of this number by applying pair-wise swaps between any 2 digits in the number as many times as he wants. Now Alice asks him to write down all the distinct numbers that he can form and sort them. Of these she wants him to tell her the 3rd Smallest and the 3rd Largest possible number(numerically).

Now the number itself might be quite large having up to 105 digits and it is also known that the number will have only \(1-9\) as digits(Alice does not like 0 for some reason).
Help Bob with this task!

Input:

First line contains T which is the number of test cases.
T lines follow each containing an integer N .

Output:

For each input N, output one line containing the 3rd Smallest Number and the 3rd Largest number separated by a space. If it doesn't exist, instead print "Not possible!"(without the quotes)

Constraints:

  • \(1 \le T \le 10\)
  • \(1 \le No \, of \, digits \, in \, N \le 10^5\)

Scoring:

  • \(1 \le T \le 10, 1 \le No \, of \, digits \, in \, N \le 10 \) (20 pts)
  • \(1 \le T \le 10, 10 \le No \, of \, digits \, in \, N \le 10^3\) (30 pts)
  • \(1 \le T \le 10, 1000 \le No \, of \, digits \, in \, N \le 10^5\) (50 pts)

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:20
90 votes
Tags:
Ad-HocEasy
Points:20
36 votes
Tags:
Ad-HocApprovedData StructuresEasyImplementationOpen