Open In App

Count of pairs in Array with product greater than 0

Last Updated : 17 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] > 0
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++) {
 
            // Increment count if
            // condition satisfied
            if (arr[i] * arr[j] > 0)
                count++;
        }
    }
 
    // Return count of pairs
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 1, -5, 0, 2, -7 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Get and print count of pairs
    cout << countPairs(arr, N);
    return 0;
}


Java




// Java code to implement the approach
 
class GFG {
 
    // Function to return the count of
    // pairs(i, j) such that arr[i]*arr[j]>0
    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++) {
 
                // Increment count if
                // condition satisfied
                if (arr[i] * arr[j] > 0)
                    cnt++;
            }
        }
 
        // Return count of pairs
        return cnt;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, -5, 0, 2, -7 };
        int N = arr.length;
 
        // Get and print count of pairs
        System.out.println(countPairs(arr, N));
    }
}


Python3




# Python3 code to implement the approach
 
# Function to return the count of pairs(i, j)
# such that arr[i] * arr[j] > 0
def countPairs(arr, n) :
    cnt = 0;
    for i in range(n - 1) :
        for j in range(i + 1, n) :
 
            # Increment count if
            # condition satisfied
            if (arr[i] * arr[j] > 0) :
                cnt += 1;
 
    # Return count of pairs
    return cnt;
 
# Driver code
if __name__ == "__main__" :
    arr = [ 1, -3, 0, 2, -1 ];
    N = len(arr);
 
    # Get and print count of pairs
    print(countPairs(arr, N));


C#




// C# program to implement
// the above approach
using System;
class GFG
{
    // Function to return the count of
    // pairs(i, j) such that arr[i]*arr[j]>0
    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++) {
 
                // Increment count if
                // condition satisfied
                if (arr[i] * arr[j] > 0)
                    cnt++;
            }
        }
 
        // Return count of pairs
        return cnt;
    }
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, -5, 0, 2, -7 };
        int N = arr.Length;
 
        // Get and print count of pairs
    Console.Write(countPairs(arr, N));
}
}
 
// This code is contributed by code_hunt.


Javascript




<script>
        // JavaScript program for the above approach
 
        // Function to return the count of pairs(i, j)
        // such that arr[i] * arr[j] > 0
        function countPairs(arr, n)
        {
            let count = 0;
            for (let i = 0; i < n - 1; i++) {
                for (let j = i + 1; j < n; j++) {
 
                    // Increment count if
                    // condition satisfied
                    if (arr[i] * arr[j] > 0)
                        count++;
                }
            }
 
            // Return count of pairs
            return count;
        }
 
        // Driver code
        let arr = [1, -5, 0, 2, -7];
        let N = arr.length;
 
        // Get and print count of pairs
        document.write(countPairs(arr, N));
 
    // This code is contributed by Potta Lokesh
 
    </script>


Output

2

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




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] > 0
long countPairs(int arr[], int n)
{
    int positive = 0;
    int negative = 0;
 
    // Count number of positives and negatives
    // in the array
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0)
            continue;
        else if (arr[i] > 0)
            positive++;
 
        else
            negative++;
    }
 
    // Total pairs due to occurrence of positive
    long pos = (positive * (positive - 1)) / 2;
 
    // Total pairs due to occurrence of negatives
    long neg = (negative * (negative - 1)) / 2;
 
    // Return count of all pairs
    return pos + neg;
}
 
// Driver code
int main()
{
    int arr[] = { 1, -5, 0, 2, -7 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Get and print count of pairs
    cout << countPairs(arr, N);
    return 0;
}


Java




// Java code to implement the approach
 
class GFG {
 
    // Function to return the count of
    // pairs(i, j) such that arr[i]*arr[j]>0
    static long countPairs(int arr[], int n)
    {
        int positive = 0;
        int negative = 0;
 
        // Count number of positives
        // and negatives in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                continue;
            else if (arr[i] > 0)
                positive++;
 
            else
                negative++;
        }
 
        // Total pairs due to occurrence
        // of positive
        long pos = (positive
                    * (positive - 1))
                   / 2;
 
        // Total pairs due to occurrence
        // of negative
        long neg = (negative
                    * (negative - 1))
                   / 2;
 
        // Return count of all pairs
        return pos + neg;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, -5, 0, 2, -7 };
        int N = arr.length;
 
        // Get and print count of pairs
        System.out.println(countPairs(arr, N));
    }
}


Python3




# Python code to implement the approach
 
# Function to return the count of pairs(i, j)
# such that arr[i] * arr[j] > 0
def countPairs(arr, n):
    positive = 0
    negative = 0
     
    # Count number of positives and negatives
    # in the array
    for i in range(n):
        if arr[i] == 0:
            continue
        elif arr[i] > 0:
            positive += 1
        else:
            negative += 1
 
    # Total pairs due to occurrence of positive
    pos = (positive * (positive - 1)) // 2
 
    # Total pairs due to occurrence of negatives
    neg = (negative * (negative - 1)) // 2
 
    # Return count of all pairs
    return pos + neg
 
# Driver code
if __name__ == '__main__':
    arr = [1, -5, 0, 2, -7]
    N = len(arr)
 
    # Get and print count of pairs
    print(countPairs(arr, N))
 
# This code is contributed by Rohit Pradhan


C#




// C# program to implement
// the above approach
using System;
class GFG
{
    // Function to return the count of
    // pairs(i, j) such that arr[i]*arr[j]>0
    static long countPairs(int[] arr, int n)
    {
        int positive = 0;
        int negative = 0;
 
        // Count number of positives
        // and negatives in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                continue;
            else if (arr[i] > 0)
                positive++;
 
            else
                negative++;
        }
 
        // Total pairs due to occurrence
        // of positive
        long pos = (positive
                    * (positive - 1))
                   / 2;
 
        // Total pairs due to occurrence
        // of negative
        long neg = (negative
                    * (negative - 1))
                   / 2;
 
        // Return count of all pairs
        return pos + neg;
    }
 
// Driver Code
public static void Main()
{
        int[] arr = { 1, -5, 0, 2, -7 };
        int N = arr.Length;
 
        // Get and print count of pairs
        Console.Write(countPairs(arr, N));
}
}
 
// This code is contributed by sanjoy_62.


Javascript




<script>
// JS code to implement the approach
 
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] > 0
function countPairs(arr, n)
{
    var positive = 0;
    var negative = 0;
 
    // Count number of positives and negatives
    // in the array
    for (var i = 0; i < n; i++) {
        if (arr[i] == 0)
            continue;
        else if (arr[i] > 0)
            positive++;
 
        else
            negative++;
    }
 
    // Total pairs due to occurrence of positive
    var pos = (positive * (positive - 1)) / 2;
 
    // Total pairs due to occurrence of negatives
    var neg = (negative * (negative - 1)) / 2;
 
    // Return count of all pairs
    return pos + neg;
}
 
// Driver code
var arr = [ 1, -5, 0, 2, -7 ];
var N = arr.length;
 
// Get and print count of pairs
document.write(countPairs(arr, N));
 
// This code is contributed by phasing17
</script>


Output

2

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads