Arithmetic operators in Java - Time & Space Complexity
We want to see how the time to do arithmetic operations changes as we use bigger numbers or more operations.
How does the program's speed change when it does addition, subtraction, multiplication, or division?
Analyze the time complexity of the following code snippet.
int a = 5;
int b = 10;
int sum = a + b;
int diff = b - a;
int product = a * b;
int quotient = b / a;
System.out.println(sum + ", " + diff + ", " + product + ", " + quotient);
This code performs basic arithmetic operations on two numbers and prints the results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Four arithmetic operations (addition, subtraction, multiplication, division).
- How many times: Each operation runs exactly once.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 |
| 100 | 4 |
| 1000 | 4 |
Pattern observation: The number of operations stays the same no matter how big the numbers are.
Time Complexity: O(1)
This means the time to do these arithmetic operations does not grow with input size; it stays constant.
[X] Wrong: "Doing arithmetic on bigger numbers takes more time because the numbers are larger."
[OK] Correct: For basic integer operations in Java, the time is constant because the processor handles fixed-size numbers quickly regardless of their value.
Understanding that simple arithmetic operations run in constant time helps you focus on the parts of code that really affect performance.
"What if we changed the code to perform arithmetic inside a loop that runs n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand the modulus operator
The modulus operator returns the remainder after division of two numbers.Step 2: Identify the correct symbol in Java
In Java, the symbol for modulus is%.Final Answer:
% -> Option DQuick Check:
Modulus operator = % [OK]
- Confusing modulus % with division /
- Using * or - instead of %
- Thinking modulus returns quotient
a and b?Solution
Step 1: Identify the multiplication operator
In Java, the multiplication operator is*.Step 2: Check the statement that uses multiplication
The statementint result = a * b;correctly multipliesaandb.Final Answer:
int result = a * b; -> Option CQuick Check:
Multiplication operator = * [OK]
- Using + instead of * for multiplication
- Confusing division / with multiplication
- Using % which is remainder, not product
int x = 10; int y = 3; System.out.println(x / y);
Solution
Step 1: Understand integer division in Java
Bothxandyare integers, so division drops the decimal part.Step 2: Calculate integer division result
10 divided by 3 is 3.333..., but integer division returns 3.Final Answer:
3 -> Option AQuick Check:
Integer division drops decimals = 3 [OK]
- Expecting decimal output with int division
- Confusing / with % operator
- Assuming automatic float conversion
int a = 5; int b = 0; int c = a / b; System.out.println(c);
Solution
Step 1: Analyze the division operation
The code dividesabyb, wherebis zero.Step 2: Understand division by zero in Java
Dividing an integer by zero causes a runtime ArithmeticException error.Final Answer:
Division by zero causes runtime error -> Option AQuick Check:
Division by zero throws error [OK]
- Ignoring division by zero error
- Thinking it returns zero or infinity
- Confusing syntax error with runtime error
result after execution?int a = 15; int b = 4; int result = a / b + a % b * 2;
Solution
Step 1: Calculate integer division and modulus
15 / 4 = 3 (integer division), 15 % 4 = 3 (remainder).Step 2: Apply operator precedence and compute expression
Expression is 3 + 3 * 2. Multiplication first: 3 * 2 = 6. Then addition: 3 + 6 = 9.Step 3: Re-check calculation
Wait, re-check: 15 / 4 = 3, 15 % 4 = 3, 3 * 2 = 6, 3 + 6 = 9.Final Answer:
9 -> Option BQuick Check:
Division + (modulus * 2) = 9 [OK]
- Adding before multiplying
- Confusing division and modulus results
- Forgetting integer division truncates decimals
Ready to prove it?
Pass the test and this pattern moves unknown
