Arithmetic operators in Go - Time & Space Complexity
We want to see how the time to run arithmetic operations changes as we do more of them.
How does the number of calculations grow when we increase the input size?
Analyze the time complexity of the following code snippet.
func sumArray(nums []int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
This code adds up all numbers in a list using arithmetic operators inside a loop.
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 list
Each number in the list causes one addition operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets bigger.
[X] Wrong: "Arithmetic operations inside a loop are instant and don't affect time."
[OK] Correct: Even simple additions take time, and doing them many times adds up as the list grows.
Understanding how loops with arithmetic operations scale helps you explain efficiency clearly and confidently.
"What if we replaced the loop with nested loops doing additions? How would the time complexity change?"
Practice
Solution
Step 1: Understand the remainder operation
The remainder operator returns what is left after division.Step 2: Identify the correct operator in Go
In Go, the remainder operator is represented by the percent sign '%'.Final Answer:
% -> Option DQuick Check:
Remainder = % [OK]
- Confusing division / with remainder %
- Using * for remainder
- Using + for remainder
Solution
Step 1: Recall Go's multiplication syntax
Go uses the asterisk '*' symbol for multiplication.Step 2: Check each option
Only 'a * b' uses the correct multiplication operator.Final Answer:
a * b -> Option CQuick Check:
Multiplication = * [OK]
- Using 'x' instead of '*'
- Using '.' or '&' for multiplication
- Confusing syntax with other languages
package main
import "fmt"
func main() {
a := 7
b := 3
fmt.Println(a / b)
}Solution
Step 1: Identify variable types and operation
Both a and b are integers, so division is integer division.Step 2: Calculate integer division result
7 divided by 3 is 2.333..., but integer division drops decimals, so result is 2.Final Answer:
2 -> Option BQuick Check:
Integer division 7/3 = 2 [OK]
- Expecting float result
- Confusing integer division with float division
- Using wrong operator
package main
import "fmt"
func main() {
x := 10
y := 0
fmt.Println(x / y)
}Solution
Step 1: Analyze the division operation
The code divides x by y, where y is zero.Step 2: Understand Go's behavior on division by zero
Dividing by zero causes a runtime panic (error) in Go.Final Answer:
Division by zero causes runtime panic -> Option AQuick Check:
Division by zero = runtime panic [OK]
- Thinking it prints 0
- Expecting compile-time error
- Ignoring runtime panic
package main
import "fmt"
func main() {
a := 15
b := 4
c := a % b
d := a / b
fmt.Println(c + d)
}Solution
Step 1: Calculate remainder c = a % b
15 % 4 equals 3 because 4*3=12 and 15-12=3.Step 2: Calculate integer division d = a / b
15 / 4 equals 3 because integer division drops decimals.Step 3: Add c + d
3 + 3 equals 6.Final Answer:
6 -> Option AQuick Check:
15 % 4 = 3, 15 / 4 = 3, 3 + 3 = 6 [OK]
- Miscomputing remainder
- Using float division instead of integer
- Adding wrong values
Ready to prove it?
Pass the test and this pattern moves unknown
