Connection Status:
Competition Arena > MaxDiffBetweenCards
SRM 830 · 2022-05-27 · by misof · Greedy, Simple Math, Sorting
Class Name: MaxDiffBetweenCards
Return Type: long
Method Name: solve
Arg Types: (int)
Problem Statement

Problem Statement

A group of kids had N blank cards. Then, the following happened.

  1. Alice wrote a digit (0-9) onto each of the cards. At least one of the digits was non-zero.
  2. Bob arranged all N cards to form a valid non-negative integer. (The integer did not have any leading zeros.)
  3. Carol rearranged all N cards to form a valid non-negative integer. (Again, the integer did not have any leading zeros. Maybe it was the same integer as Bob's, maybe not.)
  4. Derek calculated the (non-negative) difference between Bob's and Carol's integer.

Calculate and return the largest value Derek could have obtained.

Constraints

  • N will be between 1 and 17, inclusive.
Examples
0)
1
Returns: 0

With just one card Bob and Carol both have to produce the same number, and Derek's difference will be zero.

1)
2
Returns: 72

The biggest difference is produced if Alice writes the digits 9 and 1, and Bob and Carol arrange them to form the numbers 91 and 19, respectively.

2)
4
Returns: 8811

Remember that some of the digits written by Alice may be zeros. The only requirement for her is that at least one of the digits must be non-zero. (This makes sure that Bob and Carol can always produce a valid integer.)

3)
3
Returns: 801
4)
5
Returns: 89001

Submissions are judged against all 17 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class MaxDiffBetweenCards with a public method long long solve(int N) · 17 test cases · 2 s / 256 MB per case

Submitting as anonymous