2529. Maximum Count of Positive Integer and Negative Integer

LeetCode 2529: Maximum Count of Positive and Negative Integers

Featured Snippet: The LeetCode 2529 daily challenge asks us to find the maximum count of positive or negative integers in a sorted array. While a naive loop works with O(n) time complexity, the most optimal solution uses a binary search algorithm (similar to Python's bisect_left) to achieve O(log n) time complexity. Below is a complete TypeScript implementation using binary search for optimal performance.

Solving LeetCode 2529 with Binary Search

So the daily leetcode challenge can be naively solved with a loop.
But the best solution in the python category uses a built in method called bisect_left, so here is a binary search implementation of that method in JavaScript and the solution that followed.


const bisectLeft = (array, x, lo = 0, hi = array.length ) => {
    while( lo < hi ){
        const mid = lo + ((hi-lo)>>1);
        if( array[mid] < x )
            lo = mid + 1;
        else
            hi = mid;
    }
    return lo;
}
function maximumCount(nums: number[]): number {
    const firstNonNegative = bisectLeft(nums, 0)
    const firstPositive = bisectLeft(nums, 1)
    const negativeCount = firstNonNegative;
    const positiveCount = nums.length - firstPositive; 
    return Math.max( negativeCount, positiveCount );
    
};
      

Understanding the O(log n) Time Complexity Approach

When dealing with sorted arrays in algorithmic challenges, binary search is often the most efficient way to locate specific insertion points or boundaries. In the context of LeetCode 2529, we need to find the transition points between negative integers, zeros, and positive integers. Since the input array is already sorted in non-decreasing order, we can leverage the bisect_left logic to find these boundaries in logarithmic time, vastly outperforming a simple linear scan, especially for large datasets.

The first binary search locates the index of the first non-negative integer (which corresponds to the total count of negative numbers). The second binary search identifies the starting index of the first strictly positive integer. By subtracting this index from the total length of the array, we easily determine the count of positive integers. Ultimately, we return the maximum of these two counts, gracefully handling edge cases like arrays filled entirely with zeros or strictly negative values.

Implementing Python's bisect_left in JavaScript

Python developers often rely on the built-in bisect module, which provides the bisect_left function to seamlessly find the insertion point for a given element to maintain sorted order. JavaScript, unfortunately, does not include a native binary search method in its standard library. Therefore, implementing a robust custom binary search function is a critical skill for JavaScript and TypeScript developers tackling complex data structures and algorithmic puzzles.

Our custom bisectLeft implementation initializes a lower bound (lo) and an upper bound (hi). It iteratively calculates the midpoint using a bitwise shift operator for optimal performance. By comparing the middle element to our target value, we intelligently narrow the search space in half during each iteration. This guarantees an O(log n) execution time, ensuring our algorithm runs efficiently even when the input size reaches millions of elements. This method is incredibly versatile and can be reused across a wide variety of competitive programming challenges.

Frequently Asked Questions (FAQ)

What is the time complexity of this LeetCode 2529 solution?

The time complexity is O(log n) because the solution utilizes two binary search operations (the custom bisect_left implementation) to find the boundaries of negative and positive integers within the sorted array, rather than iterating through every element.

Why doesn't JavaScript have a built-in binary search?

JavaScript was originally designed for lightweight DOM manipulation and did not include an extensive standard library for advanced algorithms like Python's bisect module. Developers must implement custom binary search algorithms or rely on third-party utility libraries.

How do we handle arrays with only zeros in LeetCode 2529?

If an array consists entirely of zeros, the first binary search for 0 will return 0 (no negative numbers), and the second binary search for 1 will return the array's length (no positive numbers). The algorithm will correctly return a maximum count of 0.