Count number of pairs with positive sum in an array
Last Updated :
30 Nov, 2023
Given an array arr[] of N integers, the task is to count the number of pairs with positive sum.
Examples:
Input: arr[] = {-7, -1, 3, 2}
Output: 3
Explanation: The pairs with positive sum are: {-1, 3}, {-1, 2}, {3, 2}.
Input: arr[] = {-4, -2, 5}
Output: 2
Explanation: The pairs with positive sum are: {-4, 5}, {-2, 5}.
Naive Approach: A naive approach is to traverse each element and check if there is another number in the array arr[] which can be added to it to give the positive-sum or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountPairs( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] > 0)
count += 1;
}
}
return count;
}
int main()
{
int arr[] = { -7, -1, 3, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << CountPairs(arr, n);
return 0;
}
|
Java
class GFG {
static int CountPairs( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (arr[i] + arr[j] > 0 )
count += 1 ;
}
}
return count;
}
public static void main(String[] args)
{
int [] arr = { - 7 , - 1 , 3 , 2 };
int n = arr.length;
System.out.println(CountPairs(arr, n));
}
}
|
Python3
def CountPairs(arr, n):
count = 0
for i in range (n):
for j in range (i + 1 , n):
if (arr[i] + arr[j] > 0 ):
count + = 1
return count
if __name__ = = "__main__" :
arr = [ - 7 , - 1 , 3 , 2 ]
n = len (arr)
print (CountPairs(arr, n))
|
C#
using System;
class GFG {
static int CountPairs( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] > 0) {
count += 1;
}
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { -7, -1, 3, 2 };
int n = arr.Length;
Console.WriteLine(CountPairs(arr, n));
}
}
|
Javascript
function CountPairs(arr, n) {
let count = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (arr[i] + arr[j] > 0) {
count++;
}
}
}
return count;
}
const arr = [-7, -1, 3, 2];
const n = arr.length;
console.log(CountPairs(arr, n));
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use the Two Pointers Technique
Step-by-step approach:
- Sort the given array arr[] in increasing order.
- Take two pointers. one representing the first element and second representing the last element of the sorted array.
- If the sum of elements at these pointers is greater than 0, then the difference between the pointers will give the count of pairs with positive-sum for the element at second pointers. Decrease the second pointer by 1.
- Else increase the first pointers by 1.
- Repeat the above steps untill both pointers converge towards each other.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountPairs( int arr[], int n)
{
sort(arr, arr + n);
int count = 0;
int l = 0, r = n - 1;
while (l < r) {
if (arr[l] + arr[r] > 0) {
count += (r - l);
r--;
}
else {
l++;
}
}
return count;
}
int main()
{
int arr[] = { -7, -1, 3, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << CountPairs(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static int countPairs( int arr[], int n) {
Arrays.sort(arr);
int count = 0 ;
int l = 0 , r = n - 1 ;
while (l < r) {
if (arr[l] + arr[r] > 0 ) {
count += (r - l);
r--;
} else {
l++;
}
}
return count;
}
public static void main(String[] args) {
int arr[] = { - 7 , - 1 , 3 , 2 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def CountPairs(arr, n):
arr.sort()
count = 0
l = 0
r = n - 1
while (l < r):
if (arr[l] + arr[r] > 0 ):
count + = (r - l)
r - = 1
else :
l + = 1
return count
if __name__ = = "__main__" :
arr = [ - 7 , - 1 , 3 , 2 ]
n = len (arr)
print (CountPairs(arr, n))
|
C#
using System;
class GFG {
public static int countPairs( int [] arr)
{
Array.Sort(arr);
int count = 0;
int l = 0;
int r = arr.Length - 1;
while (l < r) {
if (arr[l] + arr[r] > 0) {
count += r - l;
r--;
}
else {
l++;
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = new int [] { -7, -1, 3, 2 };
int result = countPairs(arr);
Console.WriteLine(result);
}
}
|
Javascript
function countPairs(arr) {
arr.sort((a, b) => a - b);
let count = 0;
let l = 0;
let r = arr.length - 1;
while (l < r) {
if (arr[l] + arr[r] > 0) {
count += r - l;
r--;
} else {
l++;
}
}
return count;
}
const arr = [-7, -1, 3, 2];
const result = countPairs(arr);
console.log(result);
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...