Maximum Count of Positive Integer and Negative Integer
Easy
Topics
Given a sorted array nums in non-decreasing order, return the maximum between the number of strictly positive integers and the number of strictly negative integers. Zeros count toward neither. Aim for O(log n).
Example 1
Input: nums = [-2,-1,-1,1,2,3] Output: 3
Example 2
Input: nums = [-3,-2,-1] Output: 3
Example 3
Input: nums = [5,20,66,1314] Output: 4
Constraints
- 1 <= nums.length <= 2000
- -2000 <= nums[i] <= 2000
- nums is sorted in non-decreasing order.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Count the positives, count the negatives, return the larger. Zeros belong to neither.
Counting with two passes is O(n) and is what the shown solution does. The reason the problem exists is that the array is sorted, so both counts can be found by binary search instead: locate the first index with a value >= 0 — that index is the number of negatives — and locate the first index with a value > 0, giving positives as n - that index.
💡 Both are "first position where a monotone predicate turns true", which is what lower_bound / bisect_left computes. Reaching for a linear scan on a sorted array is the thing the problem is checking for.
Approach
negatives= index of the first value>= 0.positives=nminus the index of the first value> 0.- Return the larger.
Dry Run
nums = [-2,-1,-1,1,2,3] n = 6
first index with value >= 0 -> 3 so 3 negatives
first index with value > 0 -> 3 so 6 - 3 = 3 positives
answer max(3,3) = 3
nums = [-3,-2,-1,0,0,1,2]
first >= 0 -> 3 negatives 3
first > 0 -> 5 positives 7-5 = 2 (the two zeros counted in neither)
answer 3Complexity
- Time — O(log n) with two binary searches; the linear count shown is O(n). Both pass at these constraints, but the sortedness is there to be used.
- Space — O(1).
Common Pitfalls
- Counting zeros as positive or negative. They are neither, which is why two separate boundaries are needed rather than one split point.
- Using one search and deriving the other count as
n - negatives. That silently folds the zeros into the positives. - Scanning linearly on a sorted array — correct, but it ignores the only interesting property of the input.
- Returning the sum or the difference rather than the maximum.
Full Solution — all languages
class Solution:
def maximumCount(self, nums):
neg = sum(1 for x in nums if x < 0)
pos = sum(1 for x in nums if x > 0)
return max(neg, pos)import java.util.*;
class Solution {
public int maximumCount(int[] nums) {
int neg = 0, pos = 0;
for (int x : nums) {
if (x < 0) neg++;
else if (x > 0) pos++;
}
return Math.max(neg, pos);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int maximumCount(vector<int>& nums) {
int neg = 0, pos = 0;
for (int x : nums) {
if (x < 0) neg++;
else if (x > 0) pos++;
}
return max(neg, pos);
}
};/**
* @param {number[]} nums
* @return {number}
*/ var maximumCount = function(nums) {
let neg = 0, pos = 0;
for (const x of nums) {
if (x < 0) neg++;
else if (x > 0) pos++;
}
return Math.max(neg, pos);
};