Smallest number
Practice
3.4 (7 votes)
Algorithms
Basics of greedy algorithms
C++
Greedy algorithms
Problem
64% Success 1058 Attempts 20 Points 0.5s Time Limit 256MB Memory 1024 KB Max Code

You are given a string \(S\) that represents a number. This string consists of the following characters only:

  • \(1\)
  • \(2\)
  • \(3\)

You can perform the following operation:

  • Swap any two adjacent characters only if the absolute difference between the characters is \(1\)

Your task is to determine the smallest number that can be formed by using the provided operation. You can perform this operation any number of times (possibly zero). 

Note: Assume \(1\) based indexing.

Input format

  • The first line contains an integer \(T\) that denotes the number of test cases.
  • For each test case:
    • The first line contains an integer that denotes the length of string \(S\).
    • The second line contains a string that denotes the string \(S\).

Output format

For each test case, print a string representing the smallest number that can be formed in a new line.

Constraints

\(1 \le T \le 10 \\ 1 \le |S| \le 10^5 \)

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
8 votes
Tags:
Greedy AlgorithmsBasics of Greedy AlgorithmsAlgorithms
Points:20
13 votes
Tags:
AlgorithmsArraysBrute-force searchEasyGreedy Algorithms
Points:20
34 votes
Tags:
AlgorithmsApprovedEasyGreedy AlgorithmsOpen