Maximum Count of Positive Integer and Negative Integer

Given an array nums sorted in non-decreasing order, return the larger of two counts: the number of positive integers and the number of negative integers in nums. Zero is neither positive nor negative.

30:00

Maximum Count of Positive Integer and Negative Integer
easy
Free written solution
Topics
Companies

Given an array nums sorted in non-decreasing order, return the larger of two counts: the number of positive integers and the number of negative integers in nums. Zero is neither positive nor negative.

Example 1:
Input: {"nums":[-2,-1,-1,1,2,3]}
Output: 3
Constraints:
  • 1≤nums.length≤20001 \leq \text{nums.length} \leq 2000

  • −2000≤nums[i]≤2000-2000 \leq \text{nums}[i] \leq 2000

  • nums is sorted in non-decreasing order.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[-2,-1,-1,1,2,3]}

nums is sorted, so it splits into three blocks: negatives, zeros, positives. Two binary searches find the two block edges in O(log n).

Sorted nums (6)

-2
-1
-1
1
2
3
0
1
2
3
4
5
6

■ negative

■ zero

■ positive

□ [lo, hi)

Counts

neg
?
pos
?
Variables
VariableValue
n6
DepthFunction Call
Stack empty
0/19