Given an array arr[] of size N, the task is to find the number of distinct pairs in the array whose sum is > 0.
Examples:
Input: arr[] = { 3, -2, 1 }
Output: 2
Explanation:
There are two pairs of elements in the array whose sum is positive. They are:
{3, -2} = 1
{3, 1} = 4Input: arr[] = { -1, -1, -1, 0 }
Output: 0
Explanation:
There are no pairs of elements in the array whose sum is positive.
Naive Approach: The naive approach for this problem is to consider all the unique pairs of elements in the array. For every pair, check if the sum is positive or not.
Time Complexity: O(N2)
Efficient Approach:
- The idea is to use the concept of sorting and two pointer technique.
- For this problem, sorting is used because for the sum arr[i] + arr[j] > 0 where i, j are some random indices in the array, either arr[i] > 0 or arr[j] > 0 or both arr[i] and arr[j] > 0.
- Therefore, once the array is sorted, since we need to find the unique pairs. For every ‘i’ such that arr[i] > 0, we need to find the number of j’s such that arr[j] + arr[j] > 0.
- Here, it is easy to find the count of pairs by using two pointer technique because the array is sorted. We just need to find the leftmost position of ‘j’ for which the condition holds true. This is found using the lower_bound of -arr[i] + 1.
- For example, let the array arr[] = {-4, 4, -5, 5, 3, -2, -3, -1, 2, 1}. This array is sorted. Therefore, the array becomes, {-5, -4, -3, -2, -1, 1, 2, 3, 4, 5}. For some random i, lets assume arr[i] = 4. Therefore, the index of -3 is found in the array which is 2. Now, we can be sure that for all the values between the indices 2 and 8, the value of arr[i] + arr[j] > 0.
Below is the implementation of the above approach:
CPP
// C++ program to find the // number of pairs in the // array with the sum > 0 #include <bits/stdc++.h> using namespace std; // Function to find the number // of pairs in the array with // sum > 0 int findNumOfPair(int* a, int n) { // Sorting the given array sort(a, a + n); // Variable to store the count of pairs int ans = 0; // Loop to iterate through the array for (int i = 0; i < n; ++i) { // Ignore if the value is negative if (a[i] <= 0) continue; // Finding the index using lower_bound int j = lower_bound(a, a + n, -a[i] + 1) - a; // Finding the number of pairs between // two indices i and j ans += i - j; } return ans; } // Driver code int main() { int a[] = { 3, -2, 1 }; int n = sizeof(a) / sizeof(a[0]); int ans = findNumOfPair(a, n); cout << ans << endl; return 0; } |
Java
// Java program to find the // number of pairs in the // array with the sum > 0 import java.util.*; class GFG{ // Function to find the number // of pairs in the array with // sum > 0 static int findNumOfPair(int a[], int n) { // Sorting the given array Arrays.sort(a); // Variable to store the count of pairs int ans = 0; // Loop to iterate through the array for (int i = 0; i < n; ++i) { // Ignore if the value is negative if (a[i] <= 0) continue; // Finding the index using lower_bound // find length of array int len = a.length; int j = 0; // traverse in the array while (j < len) { // if the j-th element is n // then return the index if (a[j] == n) { return j; } else { j = j + 1; } } // Finding the number of pairs between // two indices i and j ans += i - j; } return ans; } // Driver code public static void main(String[] args) { int a[] = { 3, -2, 1 }; int n = a.length; int ans = findNumOfPair(a, n); System.out.println(ans); } } // This code is contributed by AbhiThakur |
Python3
# Python3 program to find the # number of pairs in the # array with the sum > 0 from bisect import bisect_left as lower_bound # Function to find the number # of pairs in the array with # sum > 0 def findNumOfPair(a, n): # Sorting the given array a = sorted(a) # Variable to store the count of pairs ans = 0 # Loop to iterate through the array for i in range(n): # Ignore if the value is negative if (a[i] <= 0): continue # Finding the index using lower_bound j = lower_bound(a,-a[i] + 1) # Finding the number of pairs between # two indices i and j ans += i - j return ans # Driver code if __name__ == '__main__': a=[3, -2, 1] n =len(a) ans = findNumOfPair(a, n) print(ans) # This code is contributed by mohit kumar 29 |
C#
// C# program to find the // number of pairs in the // array with the sum > 0 using System; class GFG{ // Function to find the number // of pairs in the array with // sum > 0 static int findNumOfPair(int[] a, int n) { // Sorting the given array Array.Sort(a); // Variable to store the count of pairs int ans = 0; // Loop to iterate through the array for (int i = 0; i < n; ++i) { // Ignore if the value is negative if (a[i] <= 0) continue; // Finding the index using lower_bound // find length of array int len = a.Length; int j = 0; // traverse in the array while (j < len) { // if the j-th element is n // then return the index if (a[j] == n) { return j; } else { j = j + 1; } } // Finding the number of pairs between // two indices i and j ans += i - j; } return ans; } // Driver code public static void Main() { int[] a = { 3, -2, 1 }; int n = a.Length; int ans = findNumOfPair(a, n); Console.Write(ans); } } // This code is contributed by AbhiThakur |
2
Time Complexity: O(N * log(N))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Number of pairs in an array such that product is greater than sum
- Sum of all array elements less than X and greater than Y for Q queries
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Count pairs having bitwise XOR greater than K from a given array
- Queries for greater than and not less than
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Sum of previous numbers that are greater than current number for given array
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count pairs with Bitwise XOR greater than both the elements of the pair
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Count the subarray with sum strictly greater than the sum of remaining elements
- Sum of elements in an array with frequencies greater than or equal to that element
- Check if a subarray exists with sum greater than the given Array
- Smallest subarray from a given Array with sum greater than or equal to K
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

