Inside the code editor we've tried to write a function that takes an array nums
as argument and returns true
if there exists at least one positive (greater than zero) number in the array; returns false
otherwise.
So when we called the function for [-1, 2, 3]
, we expected our code to print:
Array has positive numbers
but it seems like we made some mistakes because when we run our code, it prints:
Array doesn't have positive numbers
Assignment:
Your task is to fix our function.
The core challenge of this problem is to correctly identify if there is at least one positive number in the given array. This is a common task in array manipulation and has applications in data validation, filtering, and more. A potential pitfall is incorrectly handling the array traversal or the condition check for positive numbers.
To solve this problem, we need to iterate through the array and check each element to see if it is greater than zero. If we find such an element, we can immediately return true
. If we finish the loop without finding any positive numbers, we return false
.
A naive solution might involve checking each element but could be implemented incorrectly, as seen in the problem statement. The naive approach is not necessarily inefficient but may contain logical errors.
The optimized solution involves a single pass through the array (O(n) time complexity), checking each element. This is efficient and straightforward.
true
.false
.public class PositiveNumberChecker {
// Function to check if there is at least one positive number in the array
public static boolean hasPositiveNumber(int[] nums) {
// Iterate through each element in the array
for (int num : nums) {
// Check if the current element is greater than zero
if (num > 0) {
// If a positive number is found, return true
return true;
}
}
// If no positive number is found, return false
return false;
}
public static void main(String[] args) {
int[] nums = {-1, 2, 3};
if (hasPositiveNumber(nums)) {
System.out.println("Array has positive numbers");
} else {
System.out.println("Array doesn't have positive numbers");
}
}
}
The time complexity of this solution is O(n), where n is the number of elements in the array. This is because we potentially 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.
Consider the following edge cases:
false
.false
.false
.true
.Examples:
int[] nums1 = {};
int[] nums2 = {-1, -2, -3};
int[] nums3 = {0, 0, 0};
int[] nums4 = {-1, 0, 1};
System.out.println(hasPositiveNumber(nums1)); // false
System.out.println(hasPositiveNumber(nums2)); // false
System.out.println(hasPositiveNumber(nums3)); // false
System.out.println(hasPositiveNumber(nums4)); // true
To test the solution comprehensively, include a variety of test cases:
Using a testing framework like JUnit can help automate and manage these tests.
When approaching such problems:
In this blog post, we discussed how to identify if an array contains at least one positive number. We explored the problem, developed an efficient solution, and analyzed its complexity. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.
For further reading and practice: