Problem overview
Given a non-negative integer n, the function parity(n: Int): String returns the string "even" if n is divisible by 2 and "odd" otherwise. For n = 12 the answer is "even", because 12 divided by 2 leaves nothing over. For n = 13 it is "odd".
Approach
"Divisible by 2" means "dividing by 2 leaves a remainder of 0". Kotlin has an operator for the remainder: the modulo operator %. So 12 % 2 is 0 and 13 % 2 is 1. Every integer gives one of those two remainders when divided by 2, which is why a single test is enough: check for remainder 0, and everything else must be odd.
Step by step
- Check for even. Write an
ifwhose condition isn % 2 == 0. The remainder comes first, then the comparison with==. Leave the body empty for now. - Return "even". Inside the
if,return "even". The function ends here for every even input. - Return "odd". After the
ifblock,return "odd". This line is reached only when theifwas false, because an evennhas already returned. So noelseis needed.
The solution in Kotlin
class Solution {
fun parity(n: Int): String {
if (n % 2 == 0) {
return "even"
}
return "odd"
}
}
Details worth knowing:
%is the remainder operator. With integers it returns what is left after integer division. Here13 / 2is6and13 % 2is1.=versus==. The test uses two equals signs. One equals sign would try to assign, which Kotlin does not allow inside a condition.- Precedence.
%binds tighter than==, son % 2 == 0means(n % 2) == 0without extra parentheses. - A declared return type. The function says it returns a
String, so both paths must end in areturnof a string. The finalreturn "odd"covers the path where theifdoesn't run.
Another way to write it
Because Kotlin's if produces a value, the same function can be a single expression:
fun parity(n: Int): String = if (n % 2 == 0) "even" else "odd"
It returns even for 12 and for 0, and odd for 13. Both forms behave the same. The lesson's version, with an if followed by a plain return, is easier to follow while you are learning how the pieces fit together.
When % is new, it helps to try a few values by hand. Dividing 7 by 2 leaves 1, so 7 % 2 is 1. Dividing 100 by 7 gives 14 with 2 left over, so 100 % 7 is 2. Dividing by 2 can only leave 0 or 1, and that is why the parity test needs just one comparison.
Edge cases and common mistakes
- Zero.
0 % 2is0, soparity(0)returns"even". Zero is even. - Testing for odd with
n % 2 == 1. For the non-negative inputs of this lesson, this works. It breaks for negative numbers, because in Kotlin the remainder takes the sign of the left operand:-3 % 2is-1, not1. Testing for the remainder being zero and treating everything else as the other case avoids the problem. - Dividing instead of taking the remainder.
n / 2 == 0is true only for 0 and 1, since integer division gives the quotient. The remainder is what carries the parity. - Returning the wrong type. The checks expect the strings
"even"and"odd", in lowercase and with quotes in the code. - Printing instead of returning. Calling
printlnshows text on the console, but the function must hand the string back.
Testing
Try these inputs:
parity(12)returns"even".parity(13)returns"odd".parity(0)returns"even".parity(7)returns"odd".