Pairs with sum greater than 0 in an array Last Updated : 29 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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. Table of Content[Naive Approach] - Using Nested Loops - O(n^2) Time and O(1) space [Expected Approach] - Using Sorting and Two Pointer - O(n * log(n)) Time and O(1) Space [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. C++ #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; } Java 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)); } } Python 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)) C# 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)); } } JavaScript 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)); Output2 [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. C++ #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; } Java 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)); } } Python 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)) C# 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)); } } JavaScript 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)); Output2 Comment More infoAdvertise with us Next Article Pairs with sum greater than 0 in an array S shivamsinghal1012 Follow Improve Article Tags : Greedy Searching Sorting Competitive Programming DSA Arrays two-pointer-algorithm +3 More Practice Tags : ArraysGreedySearchingSortingtwo-pointer-algorithm +1 More Similar Reads Number of pairs in an array such that product is greater than sum Given a array a[] of non-negative integers. Count the number of pairs (i, j) in the array such that a[i] + a[j] < a[i]*a[j]. (the pair (i, j) and (j, i) are considered same and i should not be equal to j) Examples: Input : a[] = {3, 4, 5} Output : 3 Pairs are (3, 4) , (4, 5) and (3,5) Input : a[] 8 min read Print all pairs in an unsorted array with equal sum Given an unsorted array A[]. The task is to print all unique pairs in the unsorted array with equal sum. Note: Print the result in the format as shown in the below examples. Examples: Input: A[] = { 6, 4, 12, 10, 22, 54, 32, 42, 21, 11} Output: Pairs : ( 4, 12) ( 6, 10) have sum : 16 Pairs : ( 10, 2 13 min read Find all pairs with a given sum in two unsorted arrays Given two unsorted arrays of distinct elements, the task is to find all pairs from both arrays whose sum is equal to a given value X.Examples: Input: arr1[] = {-1, -2, 4, -6, 5, 7}, arr2[] = {6, 3, 4, 0} , x = 8Output: 4 4 5 3Input: arr1[] = {1, 2, 4, 5, 7}, arr2[] = {5, 6, 3, 4, 8}, x = 9Output: 1 13 min read Check if a subarray exists with sum greater than the given Array Given an array of integers arr, the task is to check if there is a subarray (except the given array) such that the sum of its elements is greater than or equal to the sum of elements of the given array. If no such subarray is possible, print No, else print Yes.Examples: Input: arr = {5, 6, 7, 8} Out 7 min read 2 Sum - Count Pairs with given Sum in Sorted Array Given a sorted array arr[] and an integer target, the task is to find the number of pairs in the array whose sum is equal to target.Examples: Input: arr[] = [-1, 1, 5, 5, 7], target = 6Output: 3Explanation: Pairs with sum 6 are (1, 5), (1, 5) and (-1, 7). Input: arr[] = [1, 1, 1, 1], target = 2Outpu 9 min read Like