Print all pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to sum ; Store counts of all elements in a dictionary ; Traverse through all the elements ; Search if a pair can be formed with arr [ i ] ; Driver code
def printPairs ( arr , n , sum ) : NEW_LINE INDENT mydict = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = sum - arr [ i ] NEW_LINE if temp in mydict : NEW_LINE INDENT count = mydict [ temp ] NEW_LINE for j in range ( count ) : NEW_LINE INDENT print ( " ( " , temp , " , ▁ " , arr [ i ] , " ) " , sep = " " , end =   ' ' ) NEW_LINE DEDENT DEDENT if arr [ i ] in mydict : NEW_LINE INDENT mydict [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mydict [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 7 , - 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE sum = 6 NEW_LINE printPairs ( arr , n , sum ) NEW_LINE DEDENT