Count of pairs in Array with product greater than 0
Last Updated :
17 May, 2022
Given an array arr[] of length N, the task is to count the number of pairs (i, j) such that arr[i] * arr[j] > 0 and 0 ≤ i < j ≤ N. It is also given that element of the array can be any positive integer including zero or can be negative integer.
Examples:
Input: arr[] = {1, -5, 0, 2, -7}
Output: 2
Explanation: Here product of (1, 2) and (-5, -7) is greater than 0.
Input: arr[] = {0, 1, 5, 9}
Output: 3
Naive approach: The basic approach is to generate all possible pairs of the array and count those pairs which satisfy the given condition.
Follow the steps mentioned below to implement the idea:
- Use a nested loop to generate all pairs.
- Check if the product of the pair is greater than 0, then increase the count.
- Return the final count after generating all pairs.
Follow the steps mentioned below to implement the approach.
C++
#include <bits/stdc++.h>
using namespace std;
long countPairs( int arr[], int n)
{
long count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if (arr[i] * arr[j] > 0)
count++;
}
}
return count;
}
int main()
{
int arr[] = { 1, -5, 0, 2, -7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, N);
return 0;
}
|
Java
class GFG {
static long countPairs( int arr[], int n)
{
long cnt = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (arr[i] * arr[j] > 0 )
cnt++;
}
}
return cnt;
}
public static void main(String[] args)
{
int arr[] = { 1 , - 5 , 0 , 2 , - 7 };
int N = arr.length;
System.out.println(countPairs(arr, N));
}
}
|
Python3
def countPairs(arr, n) :
cnt = 0 ;
for i in range (n - 1 ) :
for j in range (i + 1 , n) :
if (arr[i] * arr[j] > 0 ) :
cnt + = 1 ;
return cnt;
if __name__ = = "__main__" :
arr = [ 1 , - 3 , 0 , 2 , - 1 ];
N = len (arr);
print (countPairs(arr, N));
|
C#
using System;
class GFG
{
static long countPairs( int [] arr, int n)
{
long cnt = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if (arr[i] * arr[j] > 0)
cnt++;
}
}
return cnt;
}
public static void Main()
{
int [] arr = { 1, -5, 0, 2, -7 };
int N = arr.Length;
Console.Write(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let count = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
if (arr[i] * arr[j] > 0)
count++;
}
}
return count;
}
let arr = [1, -5, 0, 2, -7];
let N = arr.length;
document.write(countPairs(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved based on the following idea:
Multiplication of two numbers can be greater than zero only if both the numbers are negative or both the numbers are positive. And if one number is zero then it can not form pair with anyone in order to get the result as greater than zero.
To implement the idea count the positive numbers & negative numbers. Then get the number of pairs using the formula of combinatorics nC2 that is (positive*(positive-1)/2 + negative*(negative*-1)/2 ). Follow the below steps to implement the idea:
- Run a loop from i = 0 to N-1:
- Count the number of positive and negative numbers in the array (say represented by positive and negative).
- Now calculate the total pairs due to occurrence of positive and total pairs due to occurrence of negative using the above observation.
- Return the total number of occurrences.
Below is the implementation for the above implementation:
C++
#include <bits/stdc++.h>
using namespace std;
long countPairs( int arr[], int n)
{
int positive = 0;
int negative = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 0)
continue ;
else if (arr[i] > 0)
positive++;
else
negative++;
}
long pos = (positive * (positive - 1)) / 2;
long neg = (negative * (negative - 1)) / 2;
return pos + neg;
}
int main()
{
int arr[] = { 1, -5, 0, 2, -7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, N);
return 0;
}
|
Java
class GFG {
static long countPairs( int arr[], int n)
{
int positive = 0 ;
int negative = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 0 )
continue ;
else if (arr[i] > 0 )
positive++;
else
negative++;
}
long pos = (positive
* (positive - 1 ))
/ 2 ;
long neg = (negative
* (negative - 1 ))
/ 2 ;
return pos + neg;
}
public static void main(String[] args)
{
int arr[] = { 1 , - 5 , 0 , 2 , - 7 };
int N = arr.length;
System.out.println(countPairs(arr, N));
}
}
|
Python3
def countPairs(arr, n):
positive = 0
negative = 0
for i in range (n):
if arr[i] = = 0 :
continue
elif arr[i] > 0 :
positive + = 1
else :
negative + = 1
pos = (positive * (positive - 1 )) / / 2
neg = (negative * (negative - 1 )) / / 2
return pos + neg
if __name__ = = '__main__' :
arr = [ 1 , - 5 , 0 , 2 , - 7 ]
N = len (arr)
print (countPairs(arr, N))
|
C#
using System;
class GFG
{
static long countPairs( int [] arr, int n)
{
int positive = 0;
int negative = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 0)
continue ;
else if (arr[i] > 0)
positive++;
else
negative++;
}
long pos = (positive
* (positive - 1))
/ 2;
long neg = (negative
* (negative - 1))
/ 2;
return pos + neg;
}
public static void Main()
{
int [] arr = { 1, -5, 0, 2, -7 };
int N = arr.Length;
Console.Write(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
var positive = 0;
var negative = 0;
for ( var i = 0; i < n; i++) {
if (arr[i] == 0)
continue ;
else if (arr[i] > 0)
positive++;
else
negative++;
}
var pos = (positive * (positive - 1)) / 2;
var neg = (negative * (negative - 1)) / 2;
return pos + neg;
}
var arr = [ 1, -5, 0, 2, -7 ];
var N = arr.length;
document.write(countPairs(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...