Problem overview
The function lastTwoDigitSum receives a non-negative integer n with at least two digits and returns the sum of its last two digits. For n = 2379 the last two digits are 7 and 9, so the answer is 7 + 9 = 16.
The task has no string handling in it. Two arithmetic operators, remainder and integer division, pull the digits out of the number directly.
Approach
Two facts about decimal numbers do the work:
- Dividing by 10 and keeping the remainder gives the last digit.
2379 % 10is9. - Dividing by 100 and keeping the remainder gives the last two digits as one number.
2379 % 100is79.
So the plan is to cut the number down to its last two digits, 79, and then split that two-digit number into its tens digit and its ones digit. The ones digit is 79 % 10 = 9. The tens digit is 79 / 10 = 7, because dividing integers drops the fractional part and with it the ones digit.
Step by step
- Cut the number to its last two digits. Declare
lastTwoDigitsasn % 100. Everything above the hundreds place disappears. - Take the last digit. Declare
lastDigitaslastTwoDigits % 10. - Take the second-to-last digit. Declare
secondLastDigitaslastTwoDigits / 10. Both operands are integers, so Kotlin's/keeps only the whole part:79 / 10is7, not 7.9. - Add them and return the sum. The two digits are ordinary
Intvalues, so+adds them.
All three variables are declared with val, because each one is set once and never changes.
The solution in Kotlin
class Solution {
fun lastTwoDigitSum(n: Int): Int {
val lastTwoDigits = n % 100
val lastDigit = lastTwoDigits % 10
val secondLastDigit = lastTwoDigits / 10
return lastDigit + secondLastDigit
}
}
Details worth knowing:
%is the remainder operator. In Kotlin it works onIntvalues and gives what is left after a division. It is the same idea as the modulo operator in other languages, with one caveat below./on twoIntvalues is integer division. There is no rounding up and no decimal point. To get 7.9 you would need aDouble.- Inferred types. No variable has a written type. Kotlin infers
Intfor each one. - Intermediate names. The same result fits in one line:
n % 10 + n / 10 % 10. The named steps follow how you would do it on paper.
Complexity
The function does a few arithmetic operations, whatever the size of n. Time is O(1) and extra space is O(1). Converting the number to a string and reading its last two characters would also work, but it creates a string and is unnecessary here.
Edge cases and common mistakes
- Zeros among the last digits. For
n = 100the last two digits are00, solastTwoDigitsis 0 and the sum is 0. Forn = 305the last two digits are05:305 % 100is 5, the last digit is 5, the tens digit is 0, and the sum is 5. The leading zero causes no trouble, because the number is never written out as text. - Exactly two digits. For
n = 99, the remainder by 100 is 99 itself, and the sum is 18. - Dividing by 100 instead of taking the remainder.
n / 100removes the last two digits rather than keeping them. - Taking
n % 10andn / 10directly. For2379,n / 10is237, a three-digit number, not the second-to-last digit. The first% 100is what makes the second step work. - Negative numbers. The challenge only gives non-negative values. In Kotlin, the remainder takes the sign of the left operand, so for
-2379this code computes-79, then-9and-7, and returns -16.
Testing
lastTwoDigitSum(2379)returns16.lastTwoDigitSum(100)returns0.lastTwoDigitSum(99)returns18.lastTwoDigitSum(305)returns5.