# C++ Program to Count Positive and Negative Numbers in an Array

• Difficulty Level : Basic
• 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++

 `// C++ program to find the count of positive``// and negative integers in an array``#include``using` `namespace` `std;``//function to calculate the positive numbers in an array``int` `CountPositive(``int` `arr[],``int` `n){``  ``int` `p_count = 0;``  ``for``(``int` `i =0;i=0){``      ``p_count++;``    ``}``  ``}``  ``return` `p_count;``}` `//Function to print the array``void` `printArray(``int` `arr[],``int` `n){``  ``cout<<``"Array: "``;``  ``for``(``int` `i = 0; i

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++

 `// C++ program to find the count of positive``// and negative integers in an array``#include``using` `namespace` `std;``//Defining recursive function to calculate no of positive in an array``int` `CountPositive(``int` `begin,``int` `arr[],``int` `n,``int` `pos_count){``  ``if` `(begin==n)  ``//base condition``  ``return` `pos_count;``  ``if` `(arr[begin]>=0)  ``//checking whether no is positive or not``    ``pos_count++;``  ``return` `CountPositive(begin+1,arr,n,pos_count);  ``//recursive calling``}` `//Function to print the array``void` `printArray(``int` `arr[],``int` `n){``  ``cout<<``"Array: "``;``  ``for``(``int` `i = 0; i

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 ``#include ` `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= 0; });``  ``int` `neg_count = count_if(arr, arr + n, [](``int` `x) { ``return` `x < 0; });` `  ``// print the counts``  ``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 .

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach` `#include ``using` `namespace` `std;` `// Function to find last index of negative number``// in the sorted array``int` `BinarySearch(``int` `*arr, ``int` `n)``{  ``    ``int` `l = 0, r = n-1, index = -1;``// Initialize index as -1 initially``    ``while``(l <= r)``    ``{  ``        ``int` `mid = (l + r) / 2;``       ` `        ``// if arr[mid] is negative , then we will not search``        ``// in the range [l,mid-1] because last index of negative``        ``//number will be in the range [mid,r] in the sorted array``        ``if``(arr[mid]<0)``        ``{``            ``l = mid + 1;``            ``index=mid;``// updating rightmost index of``            ``// negative number every time in the sorted array``        ``}``        ``else``          ``{ ``              ``r = mid - 1;``          ``}``    ``}``  ` `    ``// return last index of negative number``    ``return` `index;``}``// Function to print array elements``void` `printArray(``int` `*arr,``int` `n)``{``  ``cout<<``"Array: "``;``  ``for``(``int` `i = 0; i

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)

My Personal Notes arrow_drop_up