fork(3) download
  1. # include <stdio.h>
  2. # define bool int
  3.  
  4. void quickSort(int *, int, int);
  5.  
  6. void hasArrayTwoCandidates(int A[], int arr_size, int sum)
  7. {
  8. int l, r;
  9.  
  10. /* Sort the elements */
  11. quickSort(A, 0, arr_size-1);
  12.  
  13. /* Now look for the two candidates in the sorted
  14.   array*/
  15. l = 0;
  16. r = arr_size-1;
  17. while(l < r)
  18. {
  19. if(A[l] + A[r] == sum)
  20. {
  21. printf(" %d %d " , A[l], A[r]);
  22. l++; r--;
  23. }
  24. else if(A[l] + A[r] < sum)
  25. l++;
  26. else // A[i] + A[j] > sum
  27. r--;
  28. }
  29.  
  30. }
  31.  
  32. /* Driver program to test above function */
  33. int main()
  34. {
  35. int A[] = {-4,-4,-4,-4,-4,1,2,3,3,4,5,6,10};
  36. int n = 6;
  37. int arr_size = 13;
  38.  
  39.  
  40. hasArrayTwoCandidates(A, arr_size, n);
  41.  
  42. getchar();
  43. return 0;
  44. }
  45.  
  46. /* FOLLOWING FUNCTIONS ARE ONLY FOR SORTING
  47.   PURPOSE */
  48. void exchange(int *a, int *b)
  49. {
  50. int temp;
  51. temp = *a;
  52. *a = *b;
  53. *b = temp;
  54. }
  55.  
  56. int partition(int A[], int si, int ei)
  57. {
  58. int x = A[ei];
  59. int i = (si - 1);
  60. int j;
  61.  
  62. for (j = si; j <= ei - 1; j++)
  63. {
  64. if(A[j] <= x)
  65. {
  66. i++;
  67. exchange(&A[i], &A[j]);
  68. }
  69. }
  70. exchange (&A[i + 1], &A[ei]);
  71. return (i + 1);
  72. }
  73.  
  74. /* Implementation of Quick Sort
  75. A[] --> Array to be sorted
  76. si --> Starting index
  77. ei --> Ending index
  78. */
  79. void quickSort(int A[], int si, int ei)
  80. {
  81. int pi; /* Partitioning index */
  82. if(si < ei)
  83. {
  84. pi = partition(A, si, ei);
  85. quickSort(A, si, pi - 1);
  86. quickSort(A, pi + 1, ei);
  87. }
  88. }
Success #stdin #stdout 0.01s 2728KB
stdin
Standard input is empty
stdout
 -4 10  1 5  2 4  3 3