fork(2) download
  1. // Forked from http://i...content-available-to-author-only...e.com/QefD9V // I don't think ideone tracks this
  2. // Originally by Kenneth: http://w...content-available-to-author-only...s.org/find-a-sorted-subsequence-of-size-3-in-linear-time/#comment-1573367479
  3. // Original had this great algorithm, but a clumsy and weird implementation (esp. the code outside the loop itself)
  4.  
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. //Find a sorted subsequence of size 3 in one pass, linear time
  11. //returns an empty list on not-found
  12. vector<int> find3IncreasingNumbers(int * arr, int n)
  13. {
  14. int min_so_far = arr[0];
  15. int c_low, c_mid; // candidates
  16. bool have_candidates = false;
  17.  
  18. for(int i = 1; i < n; ++i) {
  19. if(arr[i] <= min_so_far) // less-or-equal prevents values == min from ending up as mid candidates, without a separate else if() continue;
  20. min_so_far = arr[i];
  21. else if(!have_candidates || arr[i] <= c_mid) {
  22. // If any sequence exists with a middle-numbers we've already seen (and that we haven't already finished)
  23. // then one exists involving these candidates
  24. c_low = min_so_far;
  25. c_mid = arr[i];
  26. have_candidates = true;
  27. } else {
  28. // have candidates and arr[i] > c_mid
  29. return vector<int> ( { c_low, c_mid, arr[i] } );
  30. }
  31. }
  32.  
  33. return vector<int>(); // not-found
  34. }
  35.  
  36. int main()
  37. {
  38. int array_num = 1;
  39.  
  40. // http://stackoverflow.com/questions/20913103/is-it-possible-to-pass-a-brace-enclosed-initializer-as-a-macro-parameter
  41. #define TRYFIND(...) do { \
  42.   int arr[] = __VA_ARGS__ ; \
  43.   vector<int> resultTriple = find3IncreasingNumbers(arr, sizeof(arr)/sizeof(arr[0])); \
  44. if(resultTriple.size()) \
  45. cout<<"Result of arr" << array_num << ": " <<resultTriple[0]<<" "<<resultTriple[1]<<" "<<resultTriple[2]<<endl; \
  46. else \
  47. cout << "Did not find increasing triple in arr" << array_num << "." <<endl; \
  48. array_num++; \
  49.   }while(0)
  50.  
  51. TRYFIND( {12, 11, 10, 5, 6, 2, 30} );
  52. TRYFIND( {1, 2, 3, 4} );
  53. TRYFIND( {4, 3, 1, 2} );
  54. TRYFIND( {12, 1, 11, 10, 5, 4, 3} );
  55. TRYFIND( {12, 1, 11, 10, 5, 4, 7} );
  56. TRYFIND( {12, 11, 10, 5, 2, 4, 1, 3} );
  57. TRYFIND( {12, 11, 10, 5, 2, 4, 1, 6} );
  58. TRYFIND( {5,13,6,10,3,7,2} );
  59. TRYFIND( {1, 5, 1, 5, 2, 2, 5} );
  60. TRYFIND( {1, 5, 1, 5, 2, 1, 5} );
  61. TRYFIND( {2, 3, 1, 4} );
  62. TRYFIND( {3, 1, 2, 4} );
  63. TRYFIND( {2, 4} );
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Result of arr1: 5 6 30
Result of arr2: 1 2 3
Did not find increasing triple in arr3.
Did not find increasing triple in arr4.
Result of arr5: 1 4 7
Did not find increasing triple in arr6.
Result of arr7: 2 4 6
Result of arr8: 5 6 10
Result of arr9: 1 2 5
Result of arr10: 1 2 5
Result of arr11: 2 3 4
Result of arr12: 1 2 4
Did not find increasing triple in arr13.