Given an array nums
sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.
nums
is pos
and the number of negative integers is neg
, then return the maximum of pos
and neg
.Note that 0
is neither positive nor negative.
Example 1:
Input: nums = [-2,-1,-1,1,2,3] Output: 3 Explanation: There are 3 positive integers and 3 negative integers. The maximum count among them is 3.
Example 2:
Input: nums = [-3,-2,-1,0,0,1,2] Output: 3 Explanation: There are 2 positive integers and 3 negative integers. The maximum count among them is 3.
Example 3:
Input: nums = [5,20,66,1314] Output: 4 Explanation: There are 4 positive integers and 0 negative integers. The maximum count among them is 4.
Constraints:
1 <= nums.length <= 2000
-2000 <= nums[i] <= 2000
nums
is sorted in a non-decreasing order.Follow up: Can you solve the problem in O(log(n))
time complexity?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to find the maximum count of positive and negative numbers involves simply counting all the positive and negative numbers individually. Then, we compare these two counts to determine which is bigger.
Here's how the algorithm would work step-by-step:
def maximum_count_of_positive_integer_and_negative_integer(numbers):
positive_count = 0
negative_count = 0
for number in numbers:
# Count the number of positive integers
if number > 0:
positive_count += 1
# Count the number of negative integers.
elif number < 0:
negative_count += 1
# Determine which count is larger.
if positive_count > negative_count:
return positive_count
else:
return negative_count
The problem asks us to find whether there are more positive or negative numbers in a list. The fastest way is to count positive and negative numbers separately and then directly compare the counts.
Here's how the algorithm would work step-by-step:
def maximum_count_of_positive_integer_and_negative_integer(numbers):
positive_count = 0
negative_count = 0
for number in numbers:
if number > 0:
# Increment positive_count when we find a positive number.
positive_count += 1
elif number < 0:
# Increment negative_count when we find a negative number.
negative_count += 1
# Comparing positive and negative counts to return the maximum.
if positive_count > negative_count:
return positive_count
else:
return negative_count
Case | How to Handle |
---|---|
Empty input array | Return 0 since there are no positive or negative numbers to count. |
Array containing only zeros | Return 0 since there are no positive or negative numbers. |
Array containing only positive numbers | Return the length of the array as the positive count. |
Array containing only negative numbers | Return the absolute value of the length of the array, as the negative count. |
Array with extreme positive and negative values close to integer limits | The comparison logic should not cause integer overflow errors. |
Array with a large number of duplicates of the same positive and negative values | The counting logic should accurately reflect the number of distinct positive and negative numbers. |
Array with an equal number of positive and negative numbers | Return either the positive or negative count as they are equal; the solution should be deterministic. |
Null input | Throw an IllegalArgumentException or return 0 based on requirements. |