Skip to content

2529. Maximum Count of Positive Integer and Negative Integer 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
class Solution {
 public:
  int maximumCount(vector<int>& nums) {
    return max(ranges::count_if(nums, [](int num) { return num > 0; }),
               ranges::count_if(nums, [](int num) { return num < 0; }));
  }
};
1
2
3
4
5
6
class Solution {
  public int maximumCount(int[] nums) {
    return (int) Math.max(Arrays.stream(nums).filter(num -> num > 0).count(),
                          Arrays.stream(nums).filter(num -> num < 0).count());
  }
}
1
2
3
class Solution:
  def maximumCount(self, nums: list[int]) -> int:
    return max(sum(num > 0 for num in nums), sum(num < 0 for num in nums))