Problem overview
The function printPositiveNumbers(nums: IntArray) receives an array of integers and must print, one per line, those that are positive. A number is positive when it is strictly greater than 0, so zero does not count. The function prints and returns nothing. The numbers come out in the order they appear in the array.
For nums = [3, -2, 0, 4, -4] the output is 3 followed by 4. The values -2 and -4 are negative and 0 is neither positive nor negative, so all three are skipped.
Approach: look at each number once
The task has two parts: go through every element, and decide for each one whether to print it. A loop handles the first part and an if handles the second. Nothing needs to be remembered between elements, so no extra variables are needed.
This is a filter in its simplest form. Each element is checked on its own, which means the order of the array does not matter for correctness, and the output keeps the original order.
Step by step
- Loop over
numswithfor (num in nums). Each pass givesnumthe next element. The loop variable needs novalor type, since Kotlin knows it is anIntfrom the array. - Test the number with
if (num > 0). Use>and not>=. With>=the zero in the example would be printed too. - Print inside the
if.println(num)prints the value and a line break, so each result sits on its own line.
The solution in Kotlin
class Solution {
fun printPositiveNumbers(nums: IntArray) {
for (num in nums) {
if (num > 0) {
println(num)
}
}
}
}
Kotlin details worth knowing:
IntArray. It is Kotlin's array of primitive ints. It is writtenintArrayOf(3, -2, 0, 4, -4), and it is a different type fromArray<Int>and fromList<Int>. A function that takes anIntArraycannot be called with a list.- Looping by value.
for (num in nums)hands you the elements themselves. You only need indices if you want the position as well, in which case you can writefor (i in nums.indices)and usenums[i]. - The loop variable is read-only. Assigning to
numinside the loop does not compile, and it would not change the array anyway. - No return value. The function's return type is
Unit, which Kotlin uses when you write no return type. It prints and finishes.
Complexity
Time: O(n) for an array of n numbers. Each element is read once and compared once.
Space: O(1). Nothing is stored. The output goes straight to the console.
Edge cases and common mistakes
- Zero. It is not positive.
num >= 0is the classic slip and prints it. - An empty array. The loop body never runs, so nothing is printed. No special case is needed.
- No positive numbers. An array like
[-1, 0, -7]prints nothing. - All positive numbers. The function prints the whole array, one number per line.
- Returning instead of printing. The task asks for console output. Collecting the numbers in a list and returning it would not satisfy it.
- Using
printinstead ofprintln. The numbers would run together on one line, as in34instead of two lines. - Duplicates. A repeated positive value is printed each time it appears.
[2, 2]prints 2 twice.
Testing
[3, -2, 0, 4, -4]prints 3 and 4.[]prints nothing.[-5, -1]prints nothing.[0]prints nothing.[1, 2, 3]prints 1, 2 and 3 on separate lines.