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 {3, -2}, {3, 1} whose sum is positive.Input: arr[] = { -1, -1, -1, 0 }
Output: 0
Explanation: There are no pairs of elements in the array whose sum is positive.
Try It Yourself
Table of Content
[Naive Approach] - Using Nested Loops - O(n^2) Time and O(1) space
The idea for this naive approach is to use nested loops to check all possible pairs to find the unique pairs of elements in the array with the sum greater than 0.
#include <bits/stdc++.h>
using namespace std;
int findNumOfPair(vector<int>& arr) {
int count = 0;
int n = arr.size();
// Checking each pair
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] > 0) {
count++;
}
}
}
return count;
}
int main() {
vector<int> arr = {3, -2, 1};
cout << findNumOfPair(arr) << endl;
return 0;
}
import java.util.Arrays;
public class Main {
public static int findNumOfPair(int[] arr) {
int count = 0;
int n = arr.length;
// Checking each pair
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] > 0) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
int[] arr = {3, -2, 1};
System.out.println(findNumOfPair(arr));
}
}
def findNumOfPair(arr):
count = 0
n = len(arr)
# Checking each pair
for i in range(n):
for j in range(i + 1, n):
if arr[i] + arr[j] > 0:
count += 1
return count
arr = [3, -2, 1]
print(findNumOfPair(arr))
using System;
class Program {
public static int FindNumOfPair(int[] arr) {
int count = 0;
int n = arr.Length;
// Checking each pair
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] > 0) {
count++;
}
}
}
return count;
}
static void Main() {
int[] arr = {3, -2, 1};
Console.WriteLine(FindNumOfPair(arr));
}
}
function findNumOfPair(arr) {
let count = 0;
const n = arr.length;
// Checking each pair
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 = [3, -2, 1];
console.log(findNumOfPair(arr));
Output
2
[Expected Approach] - Using Sorting and Two Pointer - O(n * log(n)) Time and O(1) Space
The idea is to use the concept of sorting and two pointer technique. The approach first sorts the array, traverses array from both ends, i = 0, j = arr.size() - 1
- If (arr[i] + arr[j]) > 0, then arr[j] forms a pair with all elements from i to j-1.
- If (arr[i] + arr[j]) <= 0, then arr[i] cannot form a pair with sum greater than 0 with any element from index i+1 to j. So we simply do i = i + 1.
#include <bits/stdc++.h>
using namespace std;
int countPairs(vector<int>& arr) {
// Sort the array
sort(arr.begin(), arr.end());
// Traverse from both corners using two
// pointers
int i = 0, j = arr.size() - 1, res = 0;
while (i < j) {
if (arr[i] + arr[j] > 0) {
res += (j - i);
j--;
} else {
i++;
}
}
return res;
}
int main() {
vector<int> arr = {3, -2, 1};
cout << countPairs(arr) << endl;
return 0;
}
import java.util.Arrays;
public class Main {
public static int countPairs(int[] arr) {
// Sort the array
Arrays.sort(arr);
// Traverse from both corners using two pointers
int i = 0, j = arr.length - 1, res = 0;
while (i < j) {
if (arr[i] + arr[j] > 0) {
res += (j - i);
j--;
} else {
i++;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {3, -2, 1};
System.out.println(countPairs(arr));
}
}
def count_pairs(arr):
# Sort the array
arr.sort()
# Traverse from both corners using two pointers
i, j, res = 0, len(arr) - 1, 0
while i < j:
if arr[i] + arr[j] > 0:
res += (j - i)
j -= 1
else:
i += 1
return res
if __name__ == '__main__':
arr = [3, -2, 1]
print(count_pairs(arr))
using System;
using System.Linq;
class Program {
public static int CountPairs(int[] arr) {
// Sort the array
Array.Sort(arr);
// Traverse from both corners using two pointers
int i = 0, j = arr.Length - 1, res = 0;
while (i < j) {
if (arr[i] + arr[j] > 0) {
res += (j - i);
j--;
} else {
i++;
}
}
return res;
}
static void Main() {
int[] arr = {3, -2, 1};
Console.WriteLine(CountPairs(arr));
}
}
function countPairs(arr) {
// Sort the array
arr.sort((a, b) => a - b);
// Traverse from both corners using two pointers
let i = 0, j = arr.length - 1, res = 0;
while (i < j) {
if (arr[i] + arr[j] > 0) {
res += (j - i);
j--;
} else {
i++;
}
}
return res;
}
const arr = [3, -2, 1];
console.log(countPairs(arr));
Output
2