Number of pairs in an array with the sum greater than 0
Last Updated :
17 Oct, 2022
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} = 4
Input: 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
#include <bits/stdc++.h>
using namespace std;
int findNumOfPair( int * a, int n)
{
sort(a, a + n);
int ans = 0;
for ( int i = 0; i < n; ++i) {
if (a[i] <= 0)
continue ;
int j = lower_bound(a, a + n, -a[i] + 1) - a;
ans += i - j;
}
return ans;
}
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
import java.util.*;
class GFG {
static int findNumOfPair( int arr[], int n)
{
Arrays.sort(arr);
int ans = 0 ;
for ( int i = 0 ; i < n; ++i) {
if (arr[i] <= 0 )
continue ;
int minReqVal = -arr[i] + 1 ;
int j = lower_bound(arr, minReqVal);
if (j >= 0 )
ans += i - j;
}
return ans;
}
static int lower_bound( int arr[], int val)
{
int start = 0 , end = arr.length;
while (start < end) {
int mid = (start + end) >> 1 ;
if (val > arr[mid])
start = mid + 1 ;
else
end = mid;
}
if (start == arr.length)
return - 1 ;
return start;
}
public static void main(String[] args)
{
int a[] = {- 2 ,- 1 ,- 1 ,- 1 ,- 1 , 0 , 1 , 2 , 3 };
int n = a.length;
int ans = findNumOfPair(a, n);
System.out.println(ans);
}
}
|
Python3
from bisect import bisect_left as lower_bound
def findNumOfPair(a, n):
a = sorted (a)
ans = 0
for i in range (n):
if (a[i] < = 0 ):
continue
j = lower_bound(a, - a[i] + 1 )
ans + = i - j
return ans
if __name__ = = '__main__' :
a = [ 3 , - 2 , 1 ]
n = len (a)
ans = findNumOfPair(a, n)
print (ans)
|
C#
using System;
class GFG {
static int findNumOfPair( int [] arr, int n)
{
Array.Sort(arr);
int ans = 0;
for ( int i = 0; i < n; ++i) {
if (arr[i] <= 0)
continue ;
int minReqVal = -arr[i] + 1;
int j = lower_bound(arr, minReqVal);
if (j >= 0)
ans += i - j;
}
return ans;
}
static int lower_bound( int [] arr, int val)
{
int start = 0, end = arr.Length;
while (start < end) {
int mid = (start + end) >> 1;
if (val > arr[mid])
start = mid + 1;
else
end = mid;
}
if (start == arr.Length)
return -1;
return start;
}
public static void Main()
{
int [] a = { -2, 1, 3 };
int n = a.Length;
int ans = findNumOfPair(a, n);
Console.Write(ans);
}
}
|
Javascript
<script>
function findNumOfPair(arr, n)
{
arr.sort( function (a,b){ return a-b;});
let ans = 0;
for (let i = 0; i < n; ++i) {
if (arr[i] <= 0)
continue ;
let minReqVal = -arr[i] + 1;
let j = lower_bound(arr, minReqVal);
if (j >= 0)
ans += i - j;
}
return ans;
}
function lower_bound(arr,val)
{
let start = 0, end = arr.length;
while (start < end) {
let mid = (start + end) >> 1;
if (val > arr[mid])
start = mid + 1;
else
end = mid;
}
if (start == arr.length)
return -1;
return start;
}
let a=[3, -2, 1];
let n = a.length;
let ans = findNumOfPair(a, n);
document.write(ans);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...