Count number of pairs with positive sum in an array | Java implementation of the above approach ; Returns number of pairs in arr [ 0. . n - 1 ] with positive sum ; Initialize result ; Consider all possible pairs and check their sums ; If arr [ i ] & arr [ j ] form valid pair ; Driver 's Code ; Function call to find the count of pairs
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 ) ) ; } }