2529. Maximum Count of Positive Integer and Negative Integer LeetCode Solution

In this guide, you will get 2529. Maximum Count of Positive Integer and Negative Integer LeetCode Solution with the best time and space complexity. The solution to Maximum Count of Positive Integer and Negative Integer problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Count of Positive Integer and Negative Integer solution in C++
  4. Maximum Count of Positive Integer and Negative Integer solution in Java
  5. Maximum Count of Positive Integer and Negative Integer solution in Python
  6. Additional Resources
2529. Maximum Count of Positive Integer and Negative Integer LeetCode Solution image

Problem Statement of Maximum Count of Positive Integer and Negative Integer

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.

Example 1:

Input: nums = [-2,-1,-1,1,2,3]
Output: 3
Explanation: There are 3 positive integers and 3 negative integers. The maximum count among them is 3.

Example 2:

Input: nums = [-3,-2,-1,0,0,1,2]
Output: 3
Explanation: There are 2 positive integers and 3 negative integers. The maximum count among them is 3.

Example 3:

Input: nums = [5,20,66,1314]
Output: 4
Explanation: There are 4 positive integers and 0 negative integers. The maximum count among them is 4.

See also  3075. Maximize Happiness of Selected Children LeetCode Solution

Constraints:

1 <= nums.length <= 2000
-2000 <= nums[i] <= 2000
nums is sorted in a non-decreasing order.

Follow up: Can you solve the problem in O(log(n)) time complexity?

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2529. Maximum Count of Positive Integer and Negative Integer LeetCode Solution in C++

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; }));
  }
};
/* code provided by PROGIEZ */

2529. Maximum Count of Positive Integer and Negative Integer LeetCode Solution in Java

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());
  }
}
// code provided by PROGIEZ

2529. Maximum Count of Positive Integer and Negative Integer LeetCode Solution in Python

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))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.