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)?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.
readings=[-1, 2, 2, 8, 15] → 4
Four positives against one negative, so the answer is 4.
readings=[0, 0, 0] → 0
Only zeros: no positives and no negatives, so the answer is 0.
readings is sorted in non-decreasing orderreadings=[-7, -4, 0, 0, 3]
2
Run checks the first 3 tests in your browser. Submit checks all 97 tests on the server, hidden ones included.