Problem overview
Given a non-negative integer n with at least two digits, the method lastTwoDigitSum returns the sum of its last two digits. For n = 2379 the last two digits are 7 and 9, so the answer is 16.
Approach
Treating the number as text and picking out characters would work, but arithmetic is simpler. Two operators do all the work:
% 10gives the last digit:2379 % 10is9./ 10between two integers drops the last digit:2379 / 10is237.
The modulo operator % returns the remainder of a division. Dividing by 100 and keeping the remainder keeps the last two digits as a number: 2379 % 100 is 79. From a two-digit number you can read both digits the same way: the remainder after dividing by 10 is the units digit, and the quotient is the tens digit. For 79, 79 % 10 is 9 and 79 / 10 is 7.
Why the digits come out right
Our numbers are written in base 10, so dividing by 10 moves every digit one place to the right, and the digit that falls off the end is the remainder. Dividing by 100 does the same for two places. That is why % 100 keeps exactly the last two digits and discards everything to their left, however long the number is.
Step by step
- Isolate the last two digits. Declare a
valnamedlastTwoDigitsasn % 100. For 2379 it holds 79. - Take the last digit. Declare
lastDigitaslastTwoDigits % 10, which is 9. - Take the second-to-last digit. Declare
secondLastDigitaslastTwoDigits / 10, which is 7. Both operands areInt, so the division keeps only the whole part. - Add them and return the sum:
9 + 7 = 16.
The solution in Scala
class Solution {
def lastTwoDigitSum(n: Int): Int = {
val lastTwoDigits = n % 100
val lastDigit = lastTwoDigits % 10
val secondLastDigit = lastTwoDigits / 10
return lastDigit + secondLastDigit
}
}
Scala details:
valfor each step. Each digit is computed once and never changes, so an immutablevalfits. Scala infers the typeIntin every case.- Integer division. Dividing an
Intby anIntgives anIntwith the fraction dropped. If either side were aDouble,79 / 10would be7.9. - Negative numbers. In Scala, the sign of
%follows the left operand, so-2379 % 100is-79. The problem promises a non-negativen, so this doesn't arise here, but it matters if you reuse the idea. returnis optional. The final expressionlastDigit + secondLastDigitis the block's value on its own.
Complexity
Time: O(1). Three arithmetic operations and one addition, whatever the size of n.
Space: O(1). Three integers.
Edge cases and common mistakes
- Zeros in the last digits.
lastTwoDigitSum(100)is0: the last two digits are 0 and 0.lastTwoDigitSum(10)is1, because the number 10 has digits 1 and 0. - A zero tens digit. For
n = 2305,n % 100is5, the digits are 0 and 5, and the sum is 5. The same steps work:5 % 10is5and5 / 10is0. - A single digit. The problem promises two digits at least. The formula still works for one digit:
lastTwoDigitSum(7)returns7. - Mixing up the operators.
%keeps the remainder and/keeps the quotient. Using/ 100would return the leading digits instead of the trailing ones. - Adding the wrong values. Returning
lastTwoDigitsalone gives 79, not 16. The method needs the sum of the two digits.
Testing
n = 2379gives16.n = 100gives0.n = 10gives1.n = 99gives18.
