C++ Program to Count Positive and Negative Numbers in an Array
Last Updated :
27 Feb, 2023
Given an array arr of integers of the size of N, our task is to find the count of positive numbers and negative numbers in the array.
Examples:
Input: arr[] = [-9,7,-5,3,2]
Output: Positive elements = 3, Negative elements = 2
Input: arr[] = [5,4,-2,-1,-7]
Output: Positive elements = 2, Negative elements = 3
Approach:
- Traverse the elements in the array one by one.
- Find the element positive or not positive by using the condition element >=0. If the condition is satisfied increase the p_count.
- Remove the p_count from the total number of elements to get count negative numbers in the array.
- Print the positive and negative numbers count.
Example:
C++
#include<bits/stdc++.h>
using namespace std;
int CountPositive( int arr[], int n){
int p_count = 0;
for ( int i =0;i<n;i++){
if (arr[i]>=0){
p_count++;
}
}
return p_count;
}
void printArray( int arr[], int n){
cout<< "Array: " ;
for ( int i = 0; i<n; i++){
cout<<arr[i]<< " " ;
}
cout<< "\n" ;
}
int main()
{
int arr[] = {-9,7,-5,3,2 };
int n;
n = sizeof (arr) / sizeof (arr[0]);
printArray(arr, n);
int p_count = CountPositive(arr,n);
cout<< "Count of Positive elements = " <<p_count<< ", " ;
cout<< "Count of Negative elements = " <<n - p_count;
return 0;
}
|
Output
Array: -9 7 -5 3 2
Count of Positive elements = 3, Count of Negative elements = 2
Time complexity: O(n)
Auxiliary space: O(1)
Method: Using Recursion
C++
#include<bits/stdc++.h>
using namespace std;
int CountPositive( int begin, int arr[], int n, int pos_count){
if (begin==n)
return pos_count;
if (arr[begin]>=0)
pos_count++;
return CountPositive(begin+1,arr,n,pos_count);
}
void printArray( int arr[], int n){
cout<< "Array: " ;
for ( int i = 0; i<n; i++){
cout<<arr[i]<< " " ;
}
cout<< "\n" ;
}
int main()
{
int arr[] = {-9,7,-5,3,2 };
int n;
n = sizeof (arr) / sizeof (arr[0]);
printArray(arr, n);
int p_count = CountPositive(0,arr,n,0);
cout<< "Count of Positive elements = " <<p_count<< ", " ;
cout<< "Count of Negative elements = " <<n - p_count;
return 0;
}
|
Output
Array: -9 7 -5 3 2
Count of Positive elements = 3, Count of Negative elements = 2
Time complexity: O(n)
Auxiliary space: O(n)
Another approach using C++ Standard Template Library (STL) and ‘count_if()’function:
C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {-9,7,-5,3,2};
int n = sizeof (arr) / sizeof (arr[0]);
cout<< "Array: " ;
for ( int i = 0; i<n; i++){
cout<<arr[i]<< " " ;
}
cout<< "\n" ;
int pos_count = count_if(arr, arr + n, []( int x) { return x >= 0; });
int neg_count = count_if(arr, arr + n, []( int x) { return x < 0; });
cout << "Count of Positive elements =" << pos_count<< ", " ;
cout << "Count of Negative elements = " << neg_count;
return 0;
}
|
Output
Array: -9 7 -5 3 2
Count of Positive elements =3, Count of Negative elements = 2
Time complexity: O(n)
Auxiliary space: O(n)
Another Efficient Approach ( Using binary search ) :
- If the array is not sorted , then first sort the array.
- Using Binary Search to get the last index of negative number in the sorted array.
- Initialize index as last index of negative number to -1 in array because ,if there is no negative number in the array , then it will return -1 as last index of negative number.
- Count of negative numbers will be ‘ index +1 ‘ because we are using 0-based indexing
- Count of positive numbers will be ‘count of total numbers – count of negative numbers in the array .
- Print final answer .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int BinarySearch( int *arr, int n)
{
int l = 0, r = n-1, index = -1;
while (l <= r)
{
int mid = (l + r) / 2;
if (arr[mid]<0)
{
l = mid + 1;
index=mid;
}
else
{
r = mid - 1;
}
}
return index;
}
void printArray( int *arr, int n)
{
cout<< "Array: " ;
for ( int i = 0; i<n; i++)
{
cout<<arr[i]<< " " ;
}
cout<<endl;
}
int main()
{
int arr[] = {-9,7,-5,3,2 };
int n = sizeof (arr) / sizeof (arr[0]);
printArray(arr, n);
sort(arr,arr+n);
int neg_count = BinarySearch(arr, n)+1;
int pos_count = n - neg_count;
cout << "Count of Positive elements = " << pos_count<< ", " ;
cout << "Count of Negative elements = " << neg_count<<endl;
return 0;
}
|
Output
Array: -9 7 -5 3 2
Count of Positive elements = 3, Count of Negative elements = 2
Time complexity: O(n*log2n)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...