title: finding 1D peak of a given array
body: <p>I am writing the code for peak finding algorithm for a 1D array. I have read this post    <a href="https://stackoverflow.com/questions/18929108/peak-finding-algorithm">Peak finding algorithm</a>. This is exactly what I am trying to do, there is discussion about time complexity but nothing of sort of pseudocode. The problem:</p>
  
  <blockquote>
    <p>Given an array <code>[a,b,c,d,e,f,g]</code> where <code>a</code> to <code>g</code> are numbers, <code>b</code> is a peak if and only if <code>a&lt;=b</code> and <code>b&gt;=c</code>.</p>
  </blockquote>
  
  <p>Example: given an array <code>{1,2,3,4,5,19,25,20}</code>, index <code>6</code> should be returned.
  The <em>edge cases</em> should give:</p>
  
  <p><code>{100,4,3,1,19,20} -- index 0</code></p>
  
  <p><code>{1,3,5,19,20} -- index 4</code></p>
  
  <p>I have implemented in <code>Java</code>. my current run-time is <code>O(n)</code>. I am wondering if this could be improved</p>
  
  <pre><code>public static int naive(int[] arr){
          int l=arr.length;
          if (arr[0]&gt;=arr[1]) {
              return 0;
          }
          if (arr[l-1]&gt;arr[l-2]){
              return l-1;
          }
          for (int i=1; i &lt; arr.length-1;i++){
               if (arr[i] &gt;= arr[i-1] &amp;&amp; arr[i] &gt;= arr[i+1] ){
                   return i;
               }
          }
          return -1;
      }
  </code></pre>