Problem Statement
Take all the positive integers from A to B, inclusive.
Reverse as many of them as you like. (E.g., reversing 1234 changes it into 4321, and reversing 4700 turns it into 0074 = 74.)
Then, add them all up.
Calculate and return the largest possible result you can get.
Definition
- Class:
- NumReverseEasy
- Method:
- getsum
- Parameters:
- int, int
- Returns:
- long
- Method signature:
- long getsum(int A, int B)
- (be sure your method is public)
Constraints
- B will be between 1 and 100,000, inclusive.
- A will be between 1 and B, inclusive.
Examples
21
23
Returns: 75
We have the numbers 21, 22, and 23. We can reverse 23 to get 32. This will give us the final sum 21 + 22 + 32 = 75, which is the largest result we can get from these three numbers.
12
21
Returns: 489
Note that after we reverse 12, our collection of numbers will contain two separate 21s. Each of them contributes to the final sum.
97
101
Returns: 495
Here an optimal solution is not to reverse anything.
123
127
Returns: 2605
Here an optimal strategy is to reverse everything.
1
100000
Returns: 6226873030
Watch out for integer overflow.
7
99876
Returns: 6214480635
47
87654
Returns: 5065338047
3565
8766
Returns: 36490662
2
99999
Returns: 6226773029
98654
99890
Returns: 122803424
12345
54432
Returns: 2370027018