fork download
  1. #include <stdio.h>
  2. #define MAX 100000
  3.  
  4. void printPairs(int arr[], int arr_size, int sum)
  5. {
  6. int i = 0;
  7. int temp = 0;
  8. int binMap[MAX] = {0}; /*initialize hash map as 0*/
  9.  
  10. for(i = 0; i < arr_size; i++)
  11. {
  12. temp = sum - arr[i];
  13. if(temp >= 0 && binMap[temp] == 1)
  14. {
  15. printf("Pair with given sum %d is (%d, %d) \n", sum, arr[i], temp);
  16. }
  17. binMap[arr[i]] = 1;
  18. }
  19. }
  20.  
  21. /* Driver program to test above function */
  22. int main()
  23. {
  24. int A[] = {12, 4, 45, 6, 10, 12};
  25. int sum = 16;
  26. int arr_size = 6;
  27.  
  28. printPairs(A, arr_size, sum);
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 2564KB
stdin
Standard input is empty
stdout
Pair with given sum 16 is (4, 12) 
Pair with given sum 16 is (10, 6) 
Pair with given sum 16 is (12, 4)