#2529
Easy Algorithms Maximum count of positive integer and negative integer
Array Binary Search Counting
74.3% acceptance
Feb 25, 2026
1566
88
Given an array nums sorted in non-decreasing order, return the maximum between
the number of positive integers and the number of negative integers.
In other words, if the number of positive integers in nums is pos and the number
of negative integers is neg, then return the maximum of pos and neg.
Note that 0 is neither positive nor negative.
Solution
Rust
Time O(log n)
Space O(1)
impl Solution {
pub fn maximum_count(nums: Vec<i32>) -> i32 {
let neg = nums.partition_point(|&x| x < 0) as i32;
let pos = nums.len() as i32 - nums.partition_point(|&x| x <= 0) as i32;
neg.max(pos)
}
}