title: Peak and Flag Codility latest chellange
body: <p>I'm trying to solve the latest codility.com question (just for enhance my skills). I tried allot but not getting more than 30 marks there so now curious what exactly I am missing in my solution.</p>
  
  <p><strong>The question says</strong></p>
  
  <p>A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that </p>
  
  <pre><code>0 &lt; P &lt; N − 1 and A[P − 1] &lt; A[P] &gt; A[P + 1]
  </code></pre>
  
  <p>For example, the following array A:</p>
  
  <pre><code>A[0] = 1 
  A[1] = 5 
  A[2] = 3 
  A[3] = 4 
  A[4] = 3 
  A[5] = 4 
  A[6] = 1 
  A[7] = 2 
  A[8] = 3 
  A[9] = 4 
  A[10] = 6 
  A[11] = 2
  </code></pre>
  
  <p>has exactly four peaks: elements 1, 3, 5 and 10.</p>
  
  <p>You are going on a trip to a range of mountains whose relative heights are represented by array A. You have to choose how many flags you should take with you. The goal is to set the maximum number of flags on the peaks, according to certain rules.</p>
  
  <p>Flags can only be set on peaks. What's more, if you take K flags, then the distance between any two flags should be greater than or equal to K. The distance between indices P and Q is the absolute value |P − Q|.</p>
  
  <p>For example, given the mountain range represented by array A, above, with N = 12, if you take:</p>
  
  <pre><code>&gt; two flags, you can set them on peaks 1 and 5; 
  
  &gt; three flags, you can set them on peaks 1, 5 and 10; 
  
  &gt; four flags, you can set only three flags, on peaks 1, 5 and 10.
  </code></pre>
  
  <p>You can therefore set a maximum of three flags in this case.</p>
  
  <p>Write a function that, given a non-empty zero-indexed array A of N integers, returns the maximum number of flags that can be set on the peaks of the array.
  For example, given the array above</p>
  
  <p>the function should return 3, as explained above.</p>
  
  <p>Assume that:</p>
  
  <p>N is an integer within the range [1..100,000];</p>
  
  <p>each element of array A is an integer within the range [0..1,000,000,000].</p>
  
  <p>Complexity:</p>
  
  <p>expected worst-case time complexity is O(N);
  expected worst-case space complexity is O(N), beyond input storage (not counting the
  storage required for input arguments).</p>
  
  <p>So I tried this code according to my understanding of question</p>
  
  <pre><code>var A = [1,5,3,4,3,4,1,2,3,4,6,2];
  
  function solution(A) {
   array = new Array();  
     for (i = 1; i &lt; A.length - 1; i++) {  
      if (A[i - 1] &lt; A[i] &amp;&amp; A[i + 1] &lt; A[i]) {  
       array.push(i);  
      }  
     }  
  
    //console.log(A);
    //console.log(array);
    var position = array[0];
    var counter = 1;
    var len = array.length;
    for (var i = 0; i &lt; len; i++) {
      if (Math.abs(array[i+1] - position) &gt;= len) {
          position = array[i+1];
          counter ++;
          }
      }
  
    console.log("total:",counter);
    return counter;
  
  }
  </code></pre>
  
  <p>The above code works for sample array elements: <code>[1,5,3,4,3,4,1,2,3,4,6,2]</code> 
  Get peaks at indices: <code>[1, 3, 5, 10]</code> and set flags at <code>1, 5, and 10 (total 3)</code></p>
  
  <p>But codility.com says it fails on array <code>[7, 10, 4, 5, 7, 4, 6, 1, 4, 3, 3, 7]</code> 
  My code get peaks at indices: <code>[1, 4, 6, 8]</code> and set flags at 1 and 6 (total 2)
  but coditity.com says it should be 3 flags. (no idea why)
  Am I miss-understanding the question ?</p>
  
  <p>Please I am only looking for the hint/algo. I know this question is already asked by someone and solved on private chatroom but on that page I tried to get the help with that person but members rather flagging my posts as inappropriate answer so I am asking the question here again.</p>
  
  <p>P.S: You can try coding the challenge yourself <a href="https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/flags/" rel="noreferrer">here</a>! </p>