Count positives and negatives

Count positives and negatives

Easy Java Variables 24 views
Explanation Complexity

Problem Statement

Task: return [posCount, negCount, zeroCount] for the array.

Input Format

An integer n
Then n integers

Output Format

An array [posCount, negCount, zeroCount]

Example

6
3 -1 0 5 -2 0
[2, 2, 2]

Constraints

• n ≥ 0

Concept Explanation

We count how many numbers are:

• Greater than 0 → positive

• Less than 0 → negative

• Equal to 0 → zero

Step-by-Step Explanation

1.Read n and array.

2.Initialize pos = 0, neg = 0, zero = 0.

3.Loop through array:

• If value > 0 → increment pos.

• Else if value < 0 → increment neg.

• Else → increment zero.

4.Return [pos, neg, zero].

Concept Explanation

We count how many numbers are:

• Greater than 0 → positive

• Less than 0 → negative

• Equal to 0 → zero

Step-by-Step Explanation

1.Read n and array.

2.Initialize pos = 0, neg = 0, zero = 0.

3.Loop through array:

• If value > 0 → increment pos.

• Else if value < 0 → increment neg.

• Else → increment zero.

4.Return [pos, neg, zero].

Input / Output Format

Input Format
An integer n
Then n integers
Output Format
An array [posCount, negCount, zeroCount]
Constraints
• n ≥ 0

Examples

Input:
6 3 -1 0 5 -2 0
Output:
[2, 2, 2]

Example Solution (Public)

Java
static int[] countSigns(int[] a){int p=0,n=0,z=0;for(int x:a){if(x>0) p++; else if(x<0) n++; else z++;}return new int[]{p,n,z};}

Official Solution Code

static int[] countSigns(int[] a){int p=0,n=0,z=0;for(int x:a){if(x>0) p++; else if(x<0) n++; else z++;}return new int[]{p,n,z};}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.