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.

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?


Note: This problem is from LeetCode.
Compiled
Executed
Correct
program max_positive_negative
    implicit none

    integer :: i, j, k, n
    integer, parameter :: nums(4) = [-2, -1, -1, 1, 2, 3]
    integer, parameter :: nums2(6) = [-3, -2, -1, 0, 0, 1, 2]
    integer, parameter :: nums3(4) = [5, 20, 66, 1314]
    integer :: pos, neg

    write(*,*) "Example 1:"
    call get_max_pos_neg(nums, pos, neg)
    write(*,*) "Positive: ", pos, "Negative: ", neg, "Max: ", max(pos, neg)

    write(*,*) "Example 2:"
    call get_max_pos_neg(nums2, pos, neg)
    write(*,*) "Positive: ", pos, "Negative: ", neg, "Max: ", max(pos, neg)

    write(*,*) "Example 3:"
    call get_max_pos_neg(nums3, pos, neg)
    write(*,*) "Positive: ", pos, "Negative: ", neg, "Max: ", max(pos, neg)

contains

    subroutine get_max_pos_neg(nums, pos, neg)
        implicit none
        integer, intent(in) :: nums(:)
        integer, intent(out) :: pos, neg
        integer :: i

        pos = 0
        neg = 0

        do i = 1, size(nums)
            if (nums(i) > 0) then
                pos = pos + 1
            else if (nums(i) < 0) then
                neg = neg + 1
            end if
        end do

        write(*,*) "Positive: ", pos, "Negative: ", neg, "Max: ", max(pos, neg)

    end subroutine get_max_pos_neg

end program max_positive_negative
Compiled
Executed
Correct
! Declare variables
integer :: i, j, pos, neg, max_count
integer, dimension(2000) :: nums

! Read input
read *, nums

! Initialize variables
pos = 0
neg = 0
max_count = 0

! Count positive and negative integers
do i = 1, size(nums)
    if (nums(i) > 0) then
        pos = pos + 1
    else if (nums(i) < 0) then
        neg = neg + 1
    end if
end do

! Find maximum count
max_count = max(pos, neg)

! Output result
write (*,*) max_count

end
🌐 Data from online sources
def max_positive_negative_count(nums):
    pos = next((i for i, x in enumerate(nums) if x >= 0), len(nums))
    neg = len(nums) - pos
    return max(pos, neg)

The original array is sorted in non-decreasing order. We want to find the maximum count between the number of positive integers and the number of negative integers in the array. We can find the position where the first non-negative number appears. This position is the count of negative integers in the array. The count of positive integers would be the total length of the array minus this position.

In C++, we use upper_bound to find the position of the first non-negative number. In Java, Python, and JavaScript, we loop through the array and break when we find the first non-negative number.

After finding the position, we calculate the counts of both positive and negative integers, and return the maximum of the two.

🌐 Data from online sources
int maxPositiveNegativeCount(const vector<int>& nums) {
    int pos = upper_bound(nums.begin(), nums.end(), -1) - nums.begin();
    int neg = nums.size() - pos;
    return max(pos, neg);
}

The original array is sorted in non-decreasing order. We want to find the maximum count between the number of positive integers and the number of negative integers in the array. We can find the position where the first non-negative number appears. This position is the count of negative integers in the array. The count of positive integers would be the total length of the array minus this position.

In C++, we use upper_bound to find the position of the first non-negative number. In Java, Python, and JavaScript, we loop through the array and break when we find the first non-negative number.

After finding the position, we calculate the counts of both positive and negative integers, and return the maximum of the two.