fork(22) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // The function returns true if there exist three elements in AP
  5. // Assumption: set[0..n-1] is sorted
  6. bool arithmeticThree(int set[], int n)
  7. {
  8. // One by fix every element as middle element
  9. for (int j=1; j<n-1; j++)
  10. {
  11. // Initialize i and k for the current j
  12. int i = j-1, k = j+1;
  13.  
  14. // Find if there exist i and k that form AP
  15. // with j as middle element
  16. while (i >= 0 && k <= n-1)
  17. {
  18. if (set[i] + set[k] == 2*set[j])
  19. return true;
  20. (set[i] + set[k] < 2*set[j])? k++ : i--;
  21. }
  22. }
  23.  
  24. return false;
  25. }
  26.  
  27. /* Drier program to test above function*/
  28. int main()
  29. {
  30. int set1[] = {1, 7, 10, 15, 27, 29};
  31. int n1 = sizeof(set1)/sizeof(set1[0]);
  32. arithmeticThree(set1, n1)? cout << "Yes\n" : cout << "No\n";
  33.  
  34. int set2[] = {1, 7, 10, 15, 27, 28};
  35. int n2 = sizeof(set2)/sizeof(set2[0]);
  36. arithmeticThree(set2, n2)? cout << "Yes\n" : cout << "No\n";
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Yes
No