fork download
  1. #include <stdio.h>
  2.  
  3. void printPairs(int arr[], int arr_size, int sum)
  4. {
  5. int front = 0;
  6. int back = arr_size - 1;
  7.  
  8. while (front < back) {
  9. if (arr[front] + arr[back] < sum) {
  10. front++;
  11. } else if (arr[front] + arr[back] > sum) {
  12. back--;
  13. } else {
  14. printf ("Pair with given sum %d is (%d, %d) \n",
  15. sum, arr[front], arr[back]);
  16. back--;
  17. }
  18. }
  19. }
  20.  
  21. /* Driver program to test above function */
  22. int main()
  23. {
  24. int A[] = {4, 5, 6, 10, 12, 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 2292KB
stdin
Standard input is empty
stdout
Pair with given sum 16 is (4, 12) 
Pair with given sum 16 is (4, 12) 
Pair with given sum 16 is (6, 10)