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

Master LeetCode problem 2529 (Maximum Count of Positive Integer and Negative Integer), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2529. Maximum Count of Positive Integer and Negative Integer

Problem Explanation

Explanation

To solve this problem, we can iterate through the sorted array and count the number of positive and negative integers separately. Then, we simply return the maximum count between the positive and negative integers. Since the array is already sorted, we can achieve this in linear time complexity.

  • Initialize variables positiveCount and negativeCount to 0.
  • Iterate through the sorted array nums:
    • If the current element is positive, increment positiveCount.
    • If the current element is negative, increment negativeCount.
  • Return the maximum of positiveCount and negativeCount.

Time Complexity

The time complexity of this solution is O(n), where n is the number of elements in the input array nums.

Space Complexity

The space complexity is O(1) since we are using only a constant amount of extra space.

Solution Code

public int maxCount(int[] nums) {
    int positiveCount = 0;
    int negativeCount = 0;
    
    for (int num : nums) {
        if (num > 0) {
            positiveCount++;
        } else if (num < 0) {
            negativeCount++;
        }
    }
    
    return Math.max(positiveCount, negativeCount);
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2529 (Maximum Count of Positive Integer and Negative Integer)?

This page provides optimized solutions for LeetCode problem 2529 (Maximum Count of Positive Integer and Negative Integer) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2529 (Maximum Count of Positive Integer and Negative Integer)?

The time complexity for LeetCode 2529 (Maximum Count of Positive Integer and Negative Integer) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2529 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2529 in Java, C++, or Python.

Back to LeetCode Solutions