0% completed
Problem Statement
Given an array nums
sorted in increasing order, return the maximum
between the count of positive integers and the count of negative integers.
Note: 0
is neither positive nor negative.
Examples
Example 1:
- Input: nums =
[-4, -3, -1, 0, 1, 3, 5, 7]
- Expected Output:
4
- Justification: The array contains three negative integers (-4, -3, -1) and four positive integers (1, 3, 5, 7). The maximum count between negatives and positives is 4.
Example 2:
- Input: nums =
[-8, -7, -5, -4, 0, 0, 0]
- Expected Output:
4
- Justification: Here, there are four negative integers (-8, -7, -5, -4) and no positives. Thus, the maximum count is 4.
Example 3:
- Input: nums =
[0, 2, 2, 3, 3, 3, 4]
- Expected Output:
6
- Justification: This input array includes zero negative integers and six positives (2, 2, 3, 3, 3, 4). Hence, the maximum count is 6.
Constraints:
1 <= nums.length <= 2000
-2000 <= nums[i] <= 2000
nums
is sorted in a non-decreasing order.
Solution
To solve this problem, the primary approach involves efficiently counting the positive and negative numbers in the sorted array. Given the array is already sorted, we can leverage this property by using binary search to quickly determine the boundary between negative and positive integers, optimizing the count operation. This boundary identification will allow for rapid determination of the counts of negative and positive integers without having to iterate through every element, thus promising a time-efficient solution.
The sorted nature of the array ensures that once we find the first positive integer, all subsequent integers are positive, allowing this method to be particularly effective.
Step-by-step Algorithm
-
Initialization:
- Declare and initialize
start
to0
andend
tonums.length - 1
. - Initialize
maxNegatives
andmaxPositives
to0
to store the maximum counts of negative and positive numbers respectively.
- Declare and initialize
-
First Pass: Counting Negative Numbers:
- While
start
is less than or equal toend
:- Compute the midpoint
mid = start + (end - start) / 2
. - If
nums[mid] < 0
, it indicates that we still have negative numbers:- Update
maxNegatives
tomid + 1
since this counts how many elements are negative (indices from0
tomid
). - Set
start
tomid + 1
to continue searching to the right ofmid
.
- Update
- Else, adjust
end
tomid - 1
to search in the left half for more negatives.
- Compute the midpoint
- While
-
Second Pass: Counting Positive Numbers:
- Reset
start
to0
andend
tonums.length - 1
. - While
start
is less than or equal toend
:- Compute the midpoint
mid = start + (end - start) / 2
. - If
nums[mid] > 0
, it indicates that we are in the range of positive numbers:- Update
maxPositives
tonums.length - mid
to count all positive numbers frommid
to the end of the array. - Set
end
tomid - 1
to continue searching to the left ofmid
for the start of positive numbers.
- Update
- Else, adjust
start
tomid + 1
to search in the right half for the first positive number.
- Compute the midpoint
- Reset
-
Return the Maximum Count:
- Use the
Math.max
function to determine and return the maximum ofmaxNegatives
andmaxPositives
.
- Use the
Algorithm Walkthrough
Let's consider the input: nums = [-4, -3, -1, 0, 1, 3, 5, 7]
.
-
Counting Negative Numbers:
- Start with
start = 0
,end = 7
. - Iteration 1:
mid = 3
, value atmid
is0
. Adjustend
to2
. - Iteration 2:
mid = 1
, value atmid
is-3
. It is negative, so updatemaxNegatives
to2
and movestart
to2
. - Iteration 3:
mid = 2
, value atmid
is-1
. It is negative, so updatemaxNegatives
to3
and movestart
to3
. - No more elements to the left; loop exits with
maxNegatives
=3
.
- Start with
-
Counting Positive Numbers:
- Reset with
start = 0
,end = 7
. - Iteration 1:
mid = 3
, value atmid
is0
. Adjuststart
to4
. - Iteration 2:
mid = 5
, value atmid
is3
. It is positive, so updatemaxPositives
to3
and moveend
to4
. - Iteration 3:
mid = 4
, value atmid
is1
. It is positive, so updatemaxPositives
to4
and moveend
to3
. - No more elements to the right; loop exits with
maxPositives
=4
.
- Reset with
-
Maximum Count:
maxNegatives = 3
andmaxPositives = 4
.- Final result is
Math.max(3, 4)
which yields4
.
Code
Complexity Analysis
Time Complexity
The solution involves two separate binary searches over the input array:
- First Binary Search: This search identifies the count of negative numbers. It performs in O(\log n) time, where (n) is the number of elements in the array.
- Second Binary Search: This search counts the positive numbers. It also runs in O(\log n) time.
Since each binary search runs independently but sequentially, the total time complexity remains O(\log n).
Space Complexity
The solution uses a constant amount of extra space for variables such as start
, end
, mid
, maxNegatives
, and maxPositives
, regardless of the input size. Therefore, the space complexity is O(1).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible