fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int compare(const void *a, const void *b)
  5. {
  6. return (*(int *)a - *(int *)b);
  7. }
  8.  
  9. int Pasangan (int *arr, int n, int x)
  10. {
  11. int p = 0;
  12. int q = n - 1;
  13.  
  14. while (p < q)
  15. {
  16. int jumlah = arr[p] + arr[q];
  17.  
  18. if (jumlah == x)
  19. {
  20. return 1;
  21. }
  22. else if (jumlah < x)
  23. {
  24. p++;
  25. }
  26. else
  27. {
  28. q--;
  29. }
  30. }
  31.  
  32. return 0;
  33. }
  34.  
  35. int main()
  36. {
  37. int n, x;
  38.  
  39. scanf("%d", &n);
  40.  
  41. int arr[n];
  42. for (int i = 0; i < n; i++)
  43. {
  44. scanf("%d", &arr[i]);
  45. }
  46.  
  47. scanf("%d", &x);
  48.  
  49. qsort(arr, n, sizeof(int), compare);
  50.  
  51.  
  52. if (Pasangan (arr, n, x))
  53. {
  54. printf("YA\n");
  55. } else
  56. {
  57. printf("TIDAK\n");
  58. }
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 5304KB
stdin
5
2 7 1 9 10
11
stdout
YA