Arithmetic operators in Kotlin - Time & Space Complexity
We want to see how the time to run arithmetic operations changes as we use more numbers.
How does the program's work grow when doing simple math on inputs?
Analyze the time complexity of the following code snippet.
fun sumArray(numbers: IntArray): Int {
var total = 0
for (num in numbers) {
total += num
}
return total
}
This code adds up all numbers in an array using arithmetic operators.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Addition inside the loop (total += num)
- How many times: Once for each number in the array
Each new number means one more addition operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to add numbers grows in a straight line as the list gets longer.
[X] Wrong: "Arithmetic operations like addition take more time as numbers get bigger."
[OK] Correct: In most programming languages, simple arithmetic operations take constant time regardless of the number size.
Understanding how loops with arithmetic operations scale helps you explain code efficiency clearly and confidently.
"What if we replaced the loop with a recursive function doing the same additions? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of each operator
The % operator gives the remainder after division, / divides, * multiplies, and - subtracts.Step 2: Identify the operator for remainder
The % operator is specifically for remainder calculation in Kotlin.Final Answer:
The % operator -> Option BQuick Check:
Remainder operator = % [OK]
- Confusing / with %, thinking / gives remainder
- Using * or - for remainder
- Mixing up operators with similar symbols
Solution
Step 1: Recall Kotlin syntax for multiplication
Kotlin uses * for multiplication, not x or dot.Step 2: Check each option
val result = 5 * 3 uses * correctly; others are invalid syntax or different operations.Final Answer:
val result = 5 * 3 -> Option DQuick Check:
Multiplication operator = * [OK]
- Using x instead of *
- Using dot for multiplication
- Confusing division / with multiplication
val a = 10 val b = 3 println(a / b)
Solution
Step 1: Identify variable types and operation
a and b are integers, so a / b does integer division.Step 2: Calculate integer division result
10 divided by 3 is 3.333..., but integer division drops decimals, so result is 3.Final Answer:
3 -> Option AQuick Check:
Integer division drops decimals = 3 [OK]
- Expecting decimal result with integer division
- Confusing / with floating-point division
- Thinking output is 3.0 or error
val x = 7 val y = 2 val result = x / y.toDouble() println(result)
Solution
Step 1: Analyze the types involved in division
y.toDouble() converts y to Double, so x / y.toDouble() divides Int by Double.Step 2: Check Kotlin's handling of mixed numeric types
Kotlin automatically converts Int to Double for mixed operations, so no error occurs.Final Answer:
No error, code runs and prints 3.5 -> Option CQuick Check:
Mixed Int/Double division allowed = 3.5 [OK]
- Thinking toDouble() causes error
- Believing mixed types cause syntax error
- Expecting need for explicit type declaration
Solution
Step 1: Understand integer division problem
Dividing integers by integer 3 causes integer division, losing decimals.Step 2: Convert all scores to Double before division
Converting each score to Double ensures floating-point division and precise average.Step 3: Check each option
val avg = (score1.toDouble() + score2.toDouble() + score3.toDouble()) / 3 converts all scores to Double before adding and dividing, giving correct decimal average.Final Answer:
val avg = (score1.toDouble() + score2.toDouble() + score3.toDouble()) / 3 -> Option AQuick Check:
Convert all to Double before division for decimal average [OK]
- Dividing integers directly causing integer division
- Converting sum to Double after integer division
- Converting only one score to Double
Ready to prove it?
Pass the test and this pattern moves unknown
