Print Positive Numbers From Array in Java (Time Complexity: O(n))


Given an array of integers, print to the console the positive numbers, one on each line.

A number n is positive if it is strictly greater than 0.

Example:

Input: nums = [3, -2, 0, 4, -4]
Output (console):
3
4

Note:

You have to print the numbers to the console, your function shouldn't return anything.


Understanding the Problem

The core challenge of this problem is to iterate through the array and identify numbers that are strictly greater than zero. This is a common task in data filtering where we need to extract and process specific elements based on a condition.

Potential pitfalls include misunderstanding the condition for positivity (it must be strictly greater than zero, not equal to or greater than zero) and handling edge cases such as an empty array or an array with no positive numbers.

Approach

To solve this problem, we can use a simple iteration through the array. The naive approach is to check each element and print it if it is positive. This approach is already optimal with a time complexity of O(n), where n is the number of elements in the array.

Algorithm

  1. Start by iterating through each element of the array.
  2. For each element, check if it is greater than zero.
  3. If the element is positive, print it to the console.

Code Implementation

public class PrintPositiveNumbers {
    public static void printPositives(int[] nums) {
        // Iterate through each element in the array
        for (int num : nums) {
            // Check if the number is positive
            if (num > 0) {
                // Print the positive number
                System.out.println(num);
            }
        }
    }

    public static void main(String[] args) {
        // Example usage
        int[] nums = {3, -2, 0, 4, -4};
        printPositives(nums);
    }
}

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of elements in the array. This is because we need to check each element once. The space complexity is O(1) as we are not using any additional space that scales with the input size.

Edge Cases

Consider the following edge cases:

Examples:

Input: nums = []
Output: (nothing)

Input: nums = [-1, -2, -3]
Output: (nothing)

Input: nums = [1, 2, 3]
Output:
1
2
3

Testing

To test the solution comprehensively, consider using a variety of test cases:

Using a testing framework like JUnit can help automate and manage these tests effectively.

Thinking and Problem-Solving Tips

When approaching such problems, it's important to:

Practicing similar problems and studying different algorithms can help improve problem-solving skills.

Conclusion

In this blog post, we discussed how to print positive numbers from an array in Java. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong programming and problem-solving skills.

Keep practicing and exploring further to enhance your abilities!

Additional Resources