fork(5) download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. void findpair(int arr[], int len, int sum);
  7.  
  8. int main(void)
  9. {
  10. int len = 10;
  11. int arr[] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 3};
  12. findpair(arr, len, 4);
  13. }
  14.  
  15. void findpair(int arr[], int len, int sum)
  16. {
  17. std::sort(arr, arr+len);
  18. int i = 0;
  19. int j = len -1;
  20. while( i < j){
  21. while((arr[i] + arr[j]) <= sum && i < j)
  22. {
  23. if((arr[i] + arr[j]) == sum)
  24. cout << "(" << arr[i] << "," << arr[j] << ")" << endl;
  25. i++;
  26. }
  27. j--;
  28. while((arr[i] + arr[j]) >= sum && i < j)
  29. {
  30. if((arr[i] + arr[j]) == sum)
  31. cout << "(" << arr[i] << "," << arr[j] << ")" << endl;
  32. j--;
  33. }
  34. }
  35. }
Success #stdin #stdout 0.02s 2728KB
stdin
Standard input is empty
stdout
(1,3)
(1,3)
(1,3)
(2,2)
(2,2)