Maximum Count of Positive Integer and Negative Integer (LeetCode 2529): Solution and Explanation

Easy
Array
Binary Search
Counting

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

  1. Write a helper that returns the first index with nums[i] >= target, using the half-open range lo = 0, hi = n.
  2. Inside the helper, while lo < hi, check nums[mid] >= target: if true set hi = mid, else set lo = mid + 1. Return lo.
  3. neg = firstAtLeast(0), the number of values below zero.
  4. pos = n - firstAtLeast(1), the number of values above zero.
  5. Return max(neg, pos).

Step-by-step example

nums = [-2,-1,-1,1,2,3]
  1. 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.
  2. 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.
  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)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Two lower-bound searches O(log n)
Library lower bound O(log n)
Linear count O(n)
Time

O(log n)

Each of the two binary searches halves its range every step, so each takes about log2(n) steps.

Space

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.