Problem overview
The function printEvenOdd(n: Int) takes a positive integer n. For every number from 0 to n - 1, in order, it prints the word odd if the number is odd, and the number itself if it is even. It prints and returns nothing.
For n = 6 the numbers are 0 to 5, and the output is:
0
odd
2
odd
4
odd
0, 2 and 4 are even, so they are printed as numbers. 1, 3 and 5 are odd, so each is replaced by the word.
Approach: one loop, one two-way decision
The task has two parts: visit every number from 0 to n - 1, and for each one choose between two outputs. A for loop covers the first part. An if with an else covers the second, because every number falls into exactly one of the two groups, odd or even.
To decide, use the remainder operator %. Dividing a whole number by 2 leaves 1 for an odd number and 0 for an even one. So i % 2 == 1 asks "is it odd?" and the else branch handles everything else, the even numbers.
Step by step
- Loop with
for (i in 0 until n). The range0 until nstarts at 0 and leaves outn, so the last value isn - 1, as the task wants. Writing0..nwould visitntoo and print one line too many. - Inside the loop, test whether
iis odd. Writeif (i % 2 == 1)and print the string"odd"withprintln. - Add an
elsebranch that printsi. It runs when the condition is false, that is, wheniis even. Because the two branches are exclusive, each number produces exactly one line.
The solution in Kotlin
class Solution {
fun printEvenOdd(n: Int) {
for (i in 0 until n) {
if (i % 2 == 1) {
println("odd")
} else {
println(i)
}
}
}
}
Kotlin details worth knowing:
- Quotes decide the output.
println("odd")prints the word, whileprintln(i)prints the value ofi. The same function accepts both. - The loop variable is read-only.
ibelongs to the loop. You can read it freely, but not assign to it. - The comparison uses
==. A single=is assignment and is not allowed as a condition. ifis also an expression in Kotlin. You could writeprintln(if (i % 2 == 1) "odd" else i), but that mixes a string and an integer in one expression, which makes the type less clear. The plainifandelsestatement is easier to read here.
Complexity
Time: O(n). The loop runs n times and each pass does constant work.
Space: O(1). No data is stored; the output goes to the console.
Edge cases and common mistakes
- n equal to 1. The loop visits only 0, which is even, so the output is a single line:
0. - Zero comes first. It is even, so the first line is the number 0, not the word.
- Off by one.
0..nprints fornas well. Withn = 6that adds a seventh line,6. Useuntil. - Swapped branches. Printing the number when
i % 2 == 1and the word otherwise gives the opposite output. - Printing both lines. Without the
else(a second plainifthat always runs), odd numbers would print the word and then the number too. - Quoting the variable.
println("i")prints the letter i. - Negative numbers. The task's
nis positive and the loop only visits non-negative numbers, but note that in Kotlin-3 % 2is-1, so the test% 2 == 1would not recognise negative odd numbers. Testing evenness with== 0works for all integers.
Testing
n = 6prints 0, odd, 2, odd, 4, odd.n = 1prints 0.n = 2prints 0 and odd.n = 3prints 0, odd, 2.