fork download
  1. #include <stdio.h>
  2.  
  3. void printPairs(int arr[], int arr_size, int sum)
  4. {
  5. int i = 0;
  6. int temp = 0;
  7. int bitmap = 0; // all bits are 0
  8. int bitmask = 0;
  9.  
  10. for(i = 0; i < arr_size; i++)
  11. {
  12. temp = sum - arr[i];
  13.  
  14. bitmask = 0;
  15. bitmask = 1 << temp;
  16.  
  17.  
  18. if(temp >= 0 && (bitmap & bitmask))
  19. {
  20. printf("Pair with given sum %d is (%d, %d) \n", sum, arr[i], temp);
  21. }
  22. bitmap |= 1 << arr[i];
  23. }
  24. }
  25.  
  26.  
  27. /* Driver program to test above function */
  28. int main()
  29. {
  30. int A[] = {12, 4, 13, 6, 10, 3};
  31. int sum = 16;
  32. int arr_size = 6;
  33.  
  34. printPairs(A, arr_size, sum);
  35.  
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 2248KB
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 (3, 13)