Print all pairs with given sum | Java implementation of simple method to find count of pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Store counts of all elements in map m ; Traverse through all elements ; Search if a pair can be formed with arr [ i ] . ; Driver code
import java . util . * ; class GFG { static void printPairs ( int arr [ ] , int n , int sum ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = sum - arr [ i ] ; if ( mp . containsKey ( rem ) ) { int count = mp . get ( rem ) ; for ( int j = 0 ; j < count ; j ++ ) System . out . print ( " ( " + rem + " , ▁ " + arr [ i ] + " ) " + "NEW_LINE"); } if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }