title: Sorted sub-sequence of given size k
body: <p>Looking to find longest increasing sub-sequence of given size, but the following solution(found over stack overflow) works only in some cases but doesn't satisfy all the corner cases.</p>
  
  <pre><code>function increasingSubSeq(array, k) {
      if(array.length &lt; k) 
          return;
      var maxCount = 0, count = 0;
      var prev = array[0];
      var m = [];
      for(var i = 0;i &lt; array.length;i++) {
          if(prev &lt;= array[i]) {
              count++; 
              m.push(array[i]);   
              if(m.length === k){
                  break;
              }
  
          } else {
              count = 1;
          }
          if(count &gt; maxCount) 
              maxCount = count;
  
          prev = array[i];
      }
      return m;
  } 
  </code></pre>
  
  <p>Correct o/p return</p>
  
  <p>increasingSubSeq([3,1,4,2,5,1,6,4,7], 4); should return only [3,4,5,6] 
  increasingSubSeq([3,1,4,2,5,1,6,4,7], 2); should return only [3,4]</p>
  
  <p>But the following i/p returns wrong answer</p>
  
  <p>increasingSubSeq([1,5,2,3,4],4) it returns [1,5,3,4] instead of [1,2,3,4]</p>