Count number of pairs with positive sum in an array | C ++ program to count the pairs with positive sum ; Returns number of pairs in arr [ 0. . n - 1 ] with positive sum ; Sort the array in increasing order ; Intialise result ; Intialise first and second pointer ; Till the pointers doesn 't converge  traverse array to  count the pairs ; If sum of arr [ i ] && arr [ j ] > 0 , then the count of pairs with positive sum is the difference between the two pointers ; Increase the count ; Driver 's Code ; Function call to count the pairs with positive sum
#include <bits/stdc++.h> NEW_LINE 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 ; }