Divide the digits

3

6 votes
Algorithms, Basics of Greedy Algorithms, C++, Greedy Algorithms
Problem

You are given a number N

You are required to form two numbers X and Y such that:

  • The sum of frequency of each digit in X and Y is equal to frequency of that digit in N.
  • The sum of numbers X and Y must be minimum.

Your task is to determine the minimum possible sum of X and Y.

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 N.

Output format

For each test case, you are required to print the minimum possible sum of X and Y in a new line.

Constraints

1T10510N2×1018

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first test case:

  • Minimum possible sum is 25, which can be achieved if X=12, Y=13.

For the second test case:

  • Minimum possible sum is 270, which can be achieved if X=245, Y=25.
Editor Image

?