Print Even - Odd in Java (Time Complexity: O(n))
Given a positive integer n, for each number from 0 to n - 1, print "odd" if the number is odd or the number itself if it is even.
Example:
Input: n = 6 Output (console): 0 odd 2 odd 4 odd
Note:
You have to print to the console, your function shouldn't return anything.
Understanding the Problem
The core challenge of this problem is to iterate through a range of numbers and determine whether each number is odd or even. If the number is odd, we print "odd"; if it is even, we print the number itself. This problem is straightforward but helps in understanding basic control structures and conditional statements in programming.
Approach
To solve this problem, we can use a simple loop to iterate from 0 to n-1. For each number, we check if it is odd or even using the modulus operator (%). If the number is odd (i.e., number % 2 != 0), we print "odd". Otherwise, we print the number itself.
Naive Solution
The naive solution involves iterating through each number and using an if-else statement to check if the number is odd or even. This approach is already optimal for this problem since it runs in O(n) time complexity, where n is the input number.
Optimized Solution
Given the simplicity of the problem, the naive solution is already optimal. There are no further optimizations needed.
Algorithm
1. Start a loop from 0 to n-1. 2. For each number, check if it is odd or even using the modulus operator. 3. Print "odd" if the number is odd; otherwise, print the number itself.
Code Implementation
public class PrintEvenOdd {
public static void printEvenOdd(int n) {
// Loop from 0 to n-1
for (int i = 0; i < n; i++) {
// Check if the number is odd
if (i % 2 != 0) {
System.out.println("odd");
} else {
// Print the number if it is even
System.out.println(i);
}
}
}
public static void main(String[] args) {
// Example usage
printEvenOdd(6);
}
}
Complexity Analysis
The time complexity of this solution is O(n) because we are iterating through each number from 0 to n-1 exactly once. The space complexity is O(1) as we are not using any additional space that scales with the input size.
Edge Cases
1. If n = 1, the output should be "0" since 0 is even. 2. If n = 0, there should be no output as the range is empty.
Testing
To test the solution comprehensively, consider the following test cases:
- n = 6 (as given in the example)
- n = 1 (should output "0")
- n = 0 (should output nothing)
- n = 10 (should output 0, odd, 2, odd, 4, odd, 6, odd, 8, odd)
Thinking and Problem-Solving Tips
When approaching such problems, it is essential to: 1. Understand the problem requirements and constraints. 2. Break down the problem into smaller, manageable parts. 3. Use simple control structures and conditional statements effectively. 4. Test the solution with various edge cases to ensure robustness.
Conclusion
This problem helps in understanding basic iteration and conditional logic in programming. By practicing such problems, one can improve their problem-solving skills and get better at writing clean and efficient code.
Additional Resources
For further reading and practice problems, consider the following resources: 1. [LeetCode](https://leetcode.com/) 2. [HackerRank](https://www.hackerrank.com/) 3. [GeeksforGeeks](https://www.geeksforgeeks.org/)