Given an array nums
sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.
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?
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
temp.f95:5:33: 5 | integer, parameter :: nums(4) = [-2, -1, -1, 1, 2, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 6) temp.f95:6:34: 6 | integer, parameter :: nums2(6) = [-3, -2, -1, 0, 0, 1, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (6 and 7) temp.f95:11:29: 11 | call get_max_pos_neg(nums, pos, neg) | 1 Error: Symbol ‘nums’ at (1) has no IMPLICIT type; did you mean ‘nums3’? temp.f95:15:30: 15 | call get_max_pos_neg(nums2, pos, neg) | 1 Error: Symbol ‘nums2’ at (1) has no IMPLICIT type; did you mean ‘nums3’?
! 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
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x79637407b960 in ??? #1 0x79637407c4d9 in ??? #2 0x7963742d017b in ??? #3 0x7963742c9684 in ??? #4 0x7963742ca2aa in ??? #5 0x7963742cfb7a in ??? #6 0x5ae601720282 in MAIN__ #7 0x5ae6017203a6 in main
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.
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.