Count the Number of Positive and Negative Elements in an Array
Problem Statement
Given an array of integers, the task is to count how many elements are positive and how many are negative.
- A positive number is any number greater than 0
- A negative number is any number less than 0
- The value 0 is neither positive nor negative and is usually ignored unless specified otherwise
Why This Problem Is Important
This problem builds clarity on:
- Conditional logic inside loops
- Element classification
- Array traversal
- Handling edge cases like zero values
It is commonly used in:
- Data analysis and preprocessing
- Statistical calculations
- Input validation
- Filtering datasets before applying algorithms
Input and Output Format
Input
Array: [10, -5, 0, 20, -15, 7]
Output
Positive Elements Count: 3
Negative Elements Count: 2
(Zero is ignored)
Key Concepts
- Traverse each element of the array
- Use conditional checks:
- > 0 → positive
- < 0 → negative
- Maintain two counters
Step-by-Step Algorithm
- Initialize two variables:
- positiveCount = 0
- negativeCount = 0
- Traverse the array from index 0 to n-1
- For each element:
- If element > 0, increment positiveCount
- Else if element < 0, increment negativeCount
- Print both counts
Pseudocode
positive = 0
negative = 0
for each element in array:
if element > 0:
positive++
else if element < 0:
negative++
Dry Run Example
Array: [10, -5, 0, 20, -15, 7]
10 → positive = 1
-5 → negative = 1
0 → ignored
20 → positive = 2
-15 → negative = 2
7 → positive = 3
Final Result:
Positive = 3
Negative = 2
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |

Language-wise Implementation
C Implementation
#include
int main() {
int arr[] = {10, -5, 0, 20, -15, 7};
int n = sizeof(arr) / sizeof(arr[0]);
int positive = 0, negative = 0;
for(int i = 0; i < n; i++) {
if(arr[i] > 0)
positive++;
else if(arr[i] < 0)
negative++;
}
printf("Positive Elements Count: %d\n", positive);
printf("Negative Elements Count: %d", negative);
return 0;
}
Output
Positive Elements Count: 3
Negative Elements Count: 2
C++ Implementation
#include
using namespace std;
int main() {
int arr[] = {10, -5, 0, 20, -15, 7};
int n = sizeof(arr) / sizeof(arr[0]);
int positive = 0, negative = 0;
for(int i = 0; i < n; i++) {
if(arr[i] > 0)
positive++;
else if(arr[i] < 0)
negative++;
}
cout << "Positive Elements Count: " << positive << endl;
cout << "Negative Elements Count: " << negative;
return 0;
}
Output
Positive Elements Count: 3
Negative Elements Count: 2
Java Implementation
public class Main {
public static void main(String[] args) {
int[] arr = {10, -5, 0, 20, -15, 7};
int positive = 0, negative = 0;
for(int num : arr) {
if(num > 0)
positive++;
else if(num < 0)
negative++;
}
System.out.println("Positive Elements Count: " + positive);
System.out.println("Negative Elements Count: " + negative);
}
}
Output
Positive Elements Count: 3
Negative Elements Count: 2
Python Implementation
arr = [10, -5, 0, 20, -15, 7]
positive = 0
negative = 0
for num in arr:
if num > 0:
positive += 1
elif num < 0:
negative += 1
print("Positive Elements Count:", positive)
print("Negative Elements Count:", negative)
Output
Positive Elements Count: 3
Negative Elements Count: 2
C# Implementation
using System;
class Program {
static void Main() {
int[] arr = {10, -5, 0, 20, -15, 7};
int positive = 0, negative = 0;
foreach(int num in arr) {
if(num > 0)
positive++;
else if(num < 0)
negative++;
}
Console.WriteLine("Positive Elements Count: " + positive);
Console.WriteLine("Negative Elements Count: " + negative);
}
}
Output
Positive Elements Count: 3
Negative Elements Count: 2
JavaScript Implementation
let arr = [10, -5, 0, 20, -15, 7];
let positive = 0;
let negative = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0)
positive++;
else if (arr[i] < 0)
negative++;
}
console.log("Positive Elements Count:", positive);
console.log("Negative Elements Count:", negative);
Output
Positive Elements Count: 3
Negative Elements Count: 2
Common Mistakes
- Counting zero as positive or negative
- Using incorrect comparison operators
- Forgetting to initialize counters
- Using extra space unnecessarily
Interview Variations
- Count positives, negatives, and zeros
- Separate positive and negative elements
- Count even positives and odd negatives
- Count numbers within a given range
Detailed Summary
Counting positive and negative elements in an array is a fundamental classification problem that strengthens understanding of conditional checks within array traversal. By scanning the array once and maintaining counters, we efficiently categorize elements without additional memory usage. This problem reinforces clean logic, proper condition handling, and prepares the foundation for more advanced filtering, partitioning, and data analysis problems involving arrays.
Next Problem in the Series
Replace All Elements with Zero
