Print Positive Numbers From Array in JavaScript (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 specific elements based on a condition.

Common applications include filtering datasets, processing user inputs, and more.

Potential pitfalls include misunderstanding the condition (e.g., including zero or negative numbers) and not handling edge cases like an empty array.

Approach

To solve this problem, we can use a simple loop to iterate through the array and check each element. If the element is positive, we print it to the console.

Initial naive solution: We could use a for loop to iterate through the array and use an if statement to check if each number is positive. This approach is straightforward and has a time complexity of O(n), where n is the number of elements in the array.

Optimized solution: The naive solution is already optimal for this problem since we need to check each element at least once.

Algorithm

1. Iterate through each element in the array.

2. Check if the element is greater than zero.

3. If it is, print the element to the console.

Code Implementation

function printPositiveNumbers(nums) {
  // Iterate through each element in the array
  for (let i = 0; i < nums.length; i++) {
    // Check if the current element is positive
    if (nums[i] > 0) {
      // Print the positive number to the console
      console.log(nums[i]);
    }
  }
}

// Example usage:
const nums = [3, -2, 0, 4, -4];
printPositiveNumbers(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) since we are not using any additional space that scales with the input size.

Edge Cases

1. Empty array: The function should handle an empty array without errors.

2. Array with no positive numbers: The function should not print anything.

3. Array with all positive numbers: The function should print all numbers.

Testing

To test the solution comprehensively, consider the following test cases:

Use a testing framework like Jest or simply run the function with different inputs to verify the outputs.

Thinking and Problem-Solving Tips

1. Break down the problem into smaller steps and tackle each step one at a time.

2. Think about edge cases and how your solution handles them.

3. Practice similar problems to improve your problem-solving skills.

Conclusion

In this blog post, we discussed how to print positive numbers from an array in JavaScript. 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 problem-solving skills.

Keep practicing and exploring further to improve your coding abilities!

Additional Resources