Count positive, negative, and zero values
Count how many values in an integer array are positive, negative, or zero.
Course progress
Problem 53 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
numbers = [-3, 0, 8, 2, -1, 0]
Expected output
2 2 2
What is happening?
8 and 2 are positive, -3 and -1 are negative, and two entries are zero, producing the counts 2, 2, 2.
Solution strategy
Build the right mental model
Keep three counters and classify every value with an if, else-if, else chain.
Core concepts
arrays · conditions
Complexity
Time O(n), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for count-number-signs
using System;
class Program
{
static int[] CountSigns(int[] numbers)
{
int positive = 0, negative = 0, zero = 0;
foreach (int number in numbers)
{
if (number > 0) positive++;
else if (number < 0) negative++;
else zero++;
}
return new[] { positive, negative, zero };
}
static void Main()
{
Console.WriteLine(string.Join(" ", CountSigns(new[] { -3, 0, 8, 2, -1, 0 })));
}
}Knowledge check
Questions students often ask
What is the main idea behind count positive, negative, and zero values?
Keep three counters and classify every value with an if, else-if, else chain.
What is the time and space complexity of this solution?
Time O(n), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.