All challenges
Easy 0 solved 0% acceptance

Count Positive Numbers

Count the values greater than zero in a list.

What you'll learn

  • Loop over a list of numbers
  • Count values that match a condition
  • Ignore zero and negatives

Problem

Count how many numbers in the list are greater than zero. Zero is neither positive nor negative.

Implement count_positives(numbers).

Input

numbers: The list of integers.

Output

Return the count.

Constraints

0 ≤ length ≤ 100,000; each value fits in a 64-bit integer

Examples

Example 1
Input:
numbers = [3, -1, 0, 7, 2]

Expected output:
expected = 3
Input:
std::vector<long long> numbers = {3, -1, 0, 7, 2};

Expected output:
long long expected = 3;
Input:
long[] numbers = new long[] {3, -1, 0, 7, 2};

Expected output:
long expected = 3;

Explanation

3, 7 and 2 are positive: 3.

Example 2
Input:
numbers = [0, 0, -5]

Expected output:
expected = 0
Input:
std::vector<long long> numbers = {0, 0, -5};

Expected output:
long long expected = 0;
Input:
long[] numbers = new long[] {0, 0, -5};

Expected output:
long expected = 0;

Explanation

No value is above zero: 0.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Only numbers greater than zero should be counted.

Hint 2

The task is just a filtered count over the list.

Hint 3

Do not include zero, since it is neither positive nor negative.

Expected complexity

O(n) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.