Problem overview
The method 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 16.
Approach
You could turn the number into text and read the last two characters, but that is roundabout. Two integer operators do the job directly:
- The remainder operator
%gives what is left after a division.n % 10is the last digit, andn % 100is the number made of the last two digits. - Integer division
/between twoIntvalues drops the fraction.79 / 10is7, the tens digit.
For 2379: 2379 % 100 is 79, then 79 % 10 is 9 and 79 / 10 is 7. Adding them gives 16.
Step by step
- Cut the number down to two digits. Declare
lastTwoDigitsasn % 100. Everything above the last two digits is discarded. - Get the ones digit. Declare
lastDigitaslastTwoDigits % 10. - Get the tens digit. Declare
secondLastDigitaslastTwoDigits / 10. Dividing a two-digit number by 10 removes the ones digit, and because both operands are integers there is no fraction to remove. - Add and return. The two digits are ordinary integers, so
lastDigit + secondLastDigitis the answer.
The solution in Swift
class Solution {
func lastTwoDigitSum(_ n: Int) -> Int {
let lastTwoDigits = n % 100
let lastDigit = lastTwoDigits % 10
let secondLastDigit = lastTwoDigits / 10
return lastDigit + secondLastDigit
}
}
Swift details worth knowing:
- The remainder operator. Swift's
%works on integers and takes the sign of the left operand:-7 % 3is-1. The lesson's inputs are non-negative, so the sign never matters here. - Integer division truncates toward zero.
7 / 2is3, not3.5. This only happens because both operands areInt. - Constants. Each digit is computed once, so
letis the right keyword. - Inferred types. The constants are
Intbecause the expressions on the right areInt.
Why the remainder trick works
Our numbers are written in base 10, so each place is worth ten times the one to its right. Dividing by 10 shifts every digit one place to the right and drops the last one. The remainder after dividing by 10 is exactly that dropped digit. The same idea with 100 keeps the last two places. This is why % 10 and / 10 show up in almost every digit problem, such as finding the digits of a number one by one or checking whether a number is even.
A number with more digits works the same way. For n = 987654, n % 100 is 54, and the answer is 5 + 4 = 9.
Complexity
Time and space are O(1). The method does a fixed number of arithmetic operations, whether n has 2 digits or 18.
Edge cases and common mistakes
- Exactly two digits.
n = 10gives1, andn = 99gives18. - Zeros at the end.
n = 100has last two digits00, so the answer is0. Heren % 100is0, and both digits come out as0. - A zero in the tens place.
n = 205givesn % 100 = 5, so the tens digit is5 / 10 = 0and the sum is5. The formula handles this without special cases. - Dividing
nby 10 instead oflastTwoDigits.2379 / 10is237, not a digit. - Using the wrong operator.
/gives the quotient and%the remainder. Swapping them gives the wrong digits.
Testing
n = 2379gives16.n = 10gives1.n = 99gives18.n = 100gives0.
