Ctrl K

More Positives or Negatives

Easy
A weather station logged the night's temperatures into the array readings, which is already sorted in non-decreasing order. Count how many values are positive and how many are negative, and return the larger of the two counts. Zero is neither positive nor negative. A single O(n) pass is the obvious route. Can you use the fact that the array is sorted to get O(log n)?
Example 1
readings=[-7, -4, 0, 0, 3] → 2

There are two negative readings (-7 and -4) and one positive (3); zeros do not count. The answer is 2.

Example 2
readings=[-1, 2, 2, 8, 15] → 4

Four positives against one negative, so the answer is 4.

Example 3
readings=[0, 0, 0] → 0

Only zeros: no positives and no negatives, so the answer is 0.

Constraints
  • 1 ≤ len(readings) ≤ 2000
  • -2000 ≤ readings[i] ≤ 2000
  • readings is sorted in non-decreasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCommon to Three Sorted Arrays EasyA Value Shared by Every Row MediumCleanup by Unequal Pairs MediumSpells Without Neighbors MediumPair GCDs by Sorted Position Hard
loading Python…
Input
readings=[-7, -4, 0, 0, 3]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 97 tests on the server, hidden ones included.