Maximum Count of Positive Integer and Negative Integer (LeetCode 2529): Solution and Explanation
Given a sorted integer array, return the larger of the number of negative values and the number of positive values. Zeros count for neither.
Intuition
A sorted array has a fixed shape: every negative value comes first, then every zero, then every positive value. So the whole question reduces to finding two edges.
The negatives occupy indices 0 up to the first index whose value is at least 0. That index is exactly the count of negatives.
The positives start at the first index whose value is at least 1, and run to the end. Both edges are lower-bound positions, which binary search finds in O(log n) each.
Approach: Two lower-bound searches
- Write a helper that returns the first index with
nums[i] >= target, using the half-open rangelo = 0,hi = n. - Inside the helper, while
lo < hi, checknums[mid] >= target: if true sethi = mid, else setlo = mid + 1. Returnlo. neg = firstAtLeast(0), the number of values below zero.pos = n - firstAtLeast(1), the number of values above zero.- Return
max(neg, pos).
Step-by-step example
nums = [-2,-1,-1,1,2,3]
- Search for the first value >= 0 in [0, 6): mid = 3 holds 1, so hi = 3. mid = 1 holds -1, so lo = 2. mid = 2 holds -1, so lo = 3. neg = 3.
- Search for the first value >= 1 in [0, 6): the same steps land on index 3, since there are no zeros. pos = 6 - 3 = 3.
- max(3, 3) = 3.
Code: Python, Java, C++
1class Solution:
2 def maximumCount(self, nums: List[int]) -> int:
3 def first_at_least(target):
4 lo, hi = 0, len(nums)
5 while lo < hi:
6 mid = (lo + hi) // 2
7 if nums[mid] >= target:
8 hi = mid
9 else:
10 lo = mid + 1
11 return lo
12
13 neg = first_at_least(0)
14 pos = len(nums) - first_at_least(1)
15 return max(neg, pos)1class Solution {
2 public int maximumCount(int[] nums) {
3 int neg = firstAtLeast(nums, 0);
4 int pos = nums.length - firstAtLeast(nums, 1);
5 return Math.max(neg, pos);
6 }
7
8 private int firstAtLeast(int[] nums, int target) {
9 int lo = 0, hi = nums.length;
10 while (lo < hi) {
11 int mid = (lo + hi) / 2;
12 if (nums[mid] >= target) {
13 hi = mid;
14 } else {
15 lo = mid + 1;
16 }
17 }
18 return lo;
19 }
20}1class Solution {
2public:
3 int maximumCount(vector<int>& nums) {
4 int neg = firstAtLeast(nums, 0);
5 int pos = (int) nums.size() - firstAtLeast(nums, 1);
6 return max(neg, pos);
7 }
8
9private:
10 int firstAtLeast(vector<int>& nums, int target) {
11 int lo = 0, hi = nums.size();
12 while (lo < hi) {
13 int mid = (lo + hi) / 2;
14 if (nums[mid] >= target) {
15 hi = mid;
16 } else {
17 lo = mid + 1;
18 }
19 }
20 return lo;
21 }
22};Time and space complexity
O(log n)
Each of the two binary searches halves its range every step, so each takes about log2(n) steps.
O(1)
Only the loop indices are stored.
Other approaches
Linear count (time O(n), space O(1))
Walk the array once and count values below and above zero. It ignores the sorted order but is simple and fast enough for n <= 2000.
Library lower bound (time O(log n), space O(1))
Use bisect_left in Python, lower_bound in C++ or Arrays.binarySearch variants to find the two edges without writing the loop.
Common mistakes
- Counting zeros as positive or negative. Zero is neither, so the two searches must use targets 0 and 1, not the same target.
- Using a closed range with hi = n - 1. When every value is negative the edge is n, which a closed search cannot return without extra checks.
- Searching for an exact value with a standard binary search. Zero may not be in the array at all, so you need the insertion point, not a match.
Frequently asked questions
Why target 1 for the positives instead of searching for the last zero?
For integers, the first value >= 1 is the first positive value. That turns the second search into the same lower-bound helper, with no special last-occurrence logic.
What if the array has no zeros?
Then both searches return the same index, the point where negatives switch to positives, and neg + pos = n.
Is binary search worth it when n is only 2000?
Either solution passes. The problem asks for O(log n) as a follow-up, and the two-edge idea is the reusable skill.