Skip to main content
Back to problems
Leetcode
Easy
Arrays
Binary Search
Maximum Count Of Positive Integer And Negative Integer

Count how many positive and negative numbers are in a sorted array, and return the larger count.

Acceptance 0%
Problem Statement

You are given a sorted integer array nums in non-decreasing order.

Count:

  • how many elements are positive (> 0)
  • how many elements are negative (< 0)

Return the larger of the two counts.

Because the array is sorted, the negatives appear first, then any zeros, then the positives.

Input Format

  • nums: a sorted integer array in non-decreasing order

Output Format

  • Return an integer: max(count of positive numbers, count of negative numbers)

Constraints

  • 1 <= nums.length <= 2000
  • -2000 <= nums[i] <= 2000
  • nums is sorted in non-decreasing order
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [-2,-1,-1,1,2,3]

Output

3

Explanation

There are 3 negative numbers and 3 positive numbers, so the maximum count is 3.

Example 2

Input

nums = [-3,-2,-1,0,0,1,2]

Output

3

Explanation

There are 3 negative numbers and 2 positive numbers, so the answer is 3.

Show 1 more example

Example 3

Input

nums = [5,20,66,1314]

Output

4

Explanation

All numbers are positive, so the maximum count is 4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.