Leetcode 2529: Maximum Count of Positive Integer and Negative Integer

grid47
grid47
Exploring patterns and algorithms
Feb 28, 2024 4 min read

You are given an array of integers sorted in non-decreasing order. Your task is to determine the maximum count between the number of positive integers and the number of negative integers in the array. Zero is not considered positive or negative.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of integers `nums` which is sorted in non-decreasing order.
Example: [-3, -2, -1, 1, 2, 3]
Constraints:
• 1 <= nums.length <= 2000
• -2000 <= nums[i] <= 2000
• nums is sorted in non-decreasing order.
Output: Return the maximum of the count of positive integers and the count of negative integers in the array.
Example: 3
Constraints:
• The output will be a single integer representing the maximum count of positive or negative integers.
Goal: To determine the count of positive and negative integers in the array and return the maximum of the two.
Steps:
• Initialize counters for positive and negative integers.
• Iterate through the array and count the positive and negative integers.
• Return the maximum of the two counts.
Goal: The array will have a length of at least 1, and the integers will be in the range of -2000 to 2000. The array is sorted in non-decreasing order.
Steps:
• 1 <= nums.length <= 2000
• -2000 <= nums[i] <= 2000
Assumptions:
• The input array is sorted in non-decreasing order.
• Zero (0) is neither positive nor negative.
Input: [-3, -2, -1, 1, 2, 3]
Explanation: In this example, there are 3 positive integers and 3 negative integers. The maximum count between them is 3.

Input: [-5, -3, -1, 0, 2, 4, 6]
Explanation: In this example, there are 3 positive integers and 3 negative integers. The maximum count between them is 4.

Input: [0, 2, 4, 8, 10]
Explanation: In this example, there are 5 positive integers and 0 negative integers. The maximum count between them is 5.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus