2529. Maximum Count of Positive Integer and Negative Integer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int maximumCount(int[] nums) {
int negativeCount = findFirstIndex(nums, 0), positiveCount = nums.length - findFirstIndex(nums, 1);
return Math.max(negativeCount, positiveCount);
}

private int findFirstIndex(int[] nums, int target) {
int left = 0, right = nums.length - 1; // [left, right]
while (left <= right) {
int mid = (left + right) >>> 1;
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
right = mid - 1;
}
}

return left;
}
}

References

2529. Maximum Count of Positive Integer and Negative Integer