title: find the first longest ascending or descending sub-sequence in a given unsorted sequence by C++
body: <p>I am writing a C++ program that would find and print the first longest 
  ascending or descending continuous subsequence for a vector of integers. For example, 
  given a vector with</p>
  
  <pre><code> 4, 2, 1, 2, 3, 4, 3, 5, 1, 2, 4, 6, 5
  </code></pre>
  
  <p>return 1,2,3,4</p>
  
  <p>My code is as follows: </p>
  
  <p>But, I do not know how to return the first optimal solution. 
  For example, the above sequence has 1, 2, 4, 6 which is also 4 long. 
  But, we only need to return 1,2,3,4. </p>
  
  <pre><code>bool findLong(const vector&lt;int&gt; v)
  {
    if (v.size() &lt;1)
      return false;
    if (v.size() == 1){
      cout &lt;&lt; v[0] &lt;&lt; endl;
      return true;
    }
    vector::const_iterator itr_left, itr_right;
    itr_left = v.begin();
    itr_right = v.begin()+1;
    bool ascending_flag ;
    int counter =0;
    if (*itr_right &gt; *(itr_right-1)){
      bool ascending_flag = true;
      ++ascending_counter;    
    } 
    else{
      bool ascending_flag = false;
      ++descending_counter;
    }
    int longest = INT_MIN;
    Vector&lt;int&gt;::iterator longest_left = v.begin(), longest_right =v.begin(); 
    ++itr_right; 
    while (itr_right != v.end())
    {
     if (ascending_flag &amp;&amp; *itr_right &gt; *(itr_right-1))
       ++ascending_counter; 
  
     if (!ascending_flag&amp;&amp; *itr_right &lt; *(itr_right-1))     
       ++descending_counter;       
  
     if  (ascending_flag&amp;&amp; *itr_right &lt; *(itr_right-1))
     {
       if (ascending_counter &gt; longest )
       {
          longest  = ascending_counter;
          longest_left = itr_left;
          longest_right = itr_right;
      }
      itr_left = itr_right;    
      ascending_counter = 0 ;  
      ascending_flag = false;
    }
    if  (ascending_flag &amp;&amp; *itr_right &gt; *(itr_right-1))
    {
      if (descending_counter &gt; longest )
      {
          longest  = descending_counter;
          longest_left = itr_left;
          longest_right = itr_right;
      }       
      itr_left = itr_right;      
      descending_counter = 0 ;
      ascending_flag = true;
    }
    ++itr_right;
   }
   for_each( longest_left , longest_right, print);
   cout &lt;&lt; endl;
  }
  
  Void print(int i)
  {
    cout &lt;&lt; i &lt;&lt; " , " ;
  }
  </code></pre>
  
  <p>Any comments are welcome !</p>
  
  <p>Thanks !</p>