Magicsheet logo

Maximum Count of Positive Integer and Negative Integer

Easy
100%
Updated 6/1/2025

Maximum Count of Positive Integer and Negative Integer

What is this problem about?

The "Maximum Count of Positive Integer and Negative Integer" is a fundamental array problem that focuses on counting and searching. You are given an array of integers sorted in non-decreasing order. Your task is to count how many positive integers (greater than zero) and how many negative integers (less than zero) are in the array. Finally, you need to return the maximum of these two counts. Note that zero is neither positive nor negative, so it should be excluded from both counts.

Why is this asked in interviews?

While it may seem simple, the Maximum Count of Positive Integer and Negative Integer interview question is often used in early-round screenings at companies like Microsoft and Meta. The key is the "sorted" property of the array. An interviewer isn't just looking for a solution; they are looking for the most efficient solution. While a linear scan (O(n)O(n)) works, a binary search approach (O(logn)O(\log n)) demonstrates that the candidate knows how to leverage the properties of the data to achieve better performance.

Algorithmic pattern used?

The most efficient algorithmic pattern for this problem is Binary Search. Since the array is sorted, all negative integers will be at the beginning, and all positive integers will be at the end. You can use binary search twice:

  1. Find the index of the first element that is 0\ge 0. The number of negative integers is simply that index.
  2. Find the index of the first element that is >0> 0. The number of positive integers is the total length of the array minus that index. This approach ensures that even for an array with millions of elements, the counts are found almost instantaneously.

Example explanation?

Consider the sorted array: [-3, -2, -1, 0, 0, 1, 2].

  1. Total elements: 7.
  2. Find the first index where value is 0\ge 0. This is index 3 (the first zero). So, there are 3 negative integers.
  3. Find the first index where value is >0> 0. This is index 5 ( the value 1). The positive integers start at index 5 and go to 6. Count = 7 - 5 = 2.
  4. Max(3, 2) = 3. The Maximum Count of Positive Integer and Negative Integer coding problem highlights how zeros can act as a "buffer" between the two groups we care about.

Common mistakes candidates make?

A common mistake is forgetting that zero is neither positive nor negative, leading to an overcount in one of the categories. Another mistake is using a linear O(n)O(n) scan when the problem constraints or the "sorted" hint imply that O(logn)O(\log n) is expected. Candidates also sometimes struggle with the edge cases of binary search, such as when the array contains only negative numbers, only positive numbers, or only zeros.

Interview preparation tip

Whenever you see the word "sorted" in an array problem, your first thought should always be the binary search interview pattern. Practice different variations of binary search, such as "find first occurrence" or "find last occurrence," as these are essential tools for optimizing search operations in sorted data.

Similar Questions