Relational operators in Java - Time & Space Complexity
Relational operators compare values to decide true or false. We want to see how the time to do these comparisons changes as input grows.
How does the number of comparisons affect the total time?
Analyze the time complexity of the following code snippet.
int count = 0;
for (int i = 0; i < n; i++) {
if (i < 10) {
count++;
}
}
This code counts how many numbers from 0 to n-1 are less than 10 using a relational operator.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs from 0 to n-1, checking if each number is less than 10.
- How many times: The relational check happens once per loop iteration, so n times.
Each time n grows, the loop runs more times, so the number of comparisons grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with n. Double n, double the comparisons.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input size grows.
[X] Wrong: "Since the relational check is simple, it takes constant time no matter what."
[OK] Correct: While one check is quick, the total time depends on how many times it runs. More input means more checks, so total time grows with n.
Understanding how simple comparisons add up helps you explain how loops affect performance. This skill shows you can think about code efficiency clearly.
"What if we replaced the loop with two nested loops both running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of relational operators
Relational operators compare two values and return true or false based on the comparison.Step 2: Identify the operator for 'not equal'
The operator '!=' means 'not equal to' in Java.Final Answer:
!= -> Option CQuick Check:
Not equal operator = != [OK]
- Confusing '!=' with '=='
- Using '=' instead of '=='
- Mixing up '<' and '<='
a is greater than or equal to variable b in Java?Solution
Step 1: Recall the correct relational operator syntax
The operator for 'greater than or equal to' is written as '>=' in Java.Step 2: Check each option for correct syntax
a >= b uses '>=', which is correct. Other options have invalid syntax.Final Answer:
a >= b -> Option BQuick Check:
Greater or equal operator = >= [OK]
- Writing '=>' instead of '>='
- Using '=' instead of '=='
- Confusing '<=' and '=>'
int x = 5;
int y = 10;
System.out.println(x < y);
System.out.println(x == y);
System.out.println(x != y);
Solution
Step 1: Evaluate each relational expression
x is 5 and y is 10. So, x < y is true, x == y is false, and x != y is true.Step 2: Write the output in order
The printed output will be: true false true.Final Answer:
true false true -> Option DQuick Check:
5 < 10 = true, 5 == 10 = false, 5 != 10 = true [OK]
- Mixing up == and !=
- Assuming x == y is true
- Confusing output order
int a = 7;
int b = 3;
if (a <> b) {
System.out.println("Not equal");
}
Solution
Step 1: Check the relational operator used
The operator '<>' is not a valid relational operator in Java. Java uses '!=' for not equal.Step 2: Verify other syntax elements
Semicolons and print statement are correct. Variables are declared properly.Final Answer:
The operator '<>' is invalid in Java -> Option AQuick Check:
Use '!=' for not equal, not '<>' [OK]
- Using '<>' instead of '!='
- Forgetting semicolons
- Incorrect print syntax
int a = 4;
int b = 4;
int c = 5;
boolean result = (a <= b) && (c > b);
System.out.println(result);
Choose the correct output.
Solution
Step 1: Evaluate each relational expression
a <= b is 4 <= 4, which is true. c > b is 5 > 4, which is true.Step 2: Evaluate the logical AND operation
Both conditions are true, so (true && true) is true.Final Answer:
true -> Option AQuick Check:
4 <= 4 and 5 > 4 = true [OK]
- Confusing && with ||
- Misreading <= as < only
- Assuming false if equal values
Ready to prove it?
Pass the test and this pattern moves unknown
