title: Finding peak values from an array in a given range
body: <p>The method <code>getPeakCount</code> takes an <code>int</code> array and a range (<code>int</code>) as an input and returns the number of integers that are greater than all the elements to either side for the given range.</p>
  
  <p>For example, consider an array <code>{1,4,2,6,4,5,10,8,7,11}</code> and range <code>2</code>. The result should be <code>3</code>, as <code>{..,4,2,6,4,5,..}</code>, <code>{..,4,5,10,8,7,..}</code> and <code>{..,8,7,11}</code> satisfy this condition. These satisfy the condition because <code>6</code>, <code>10</code> and <code>11</code> are all greater than the 2 elements to both their left and right.</p>
  
  <p>Note that for the the corner elements like <code>1</code> and <code>11</code>, there's no need to check the left and right side respectively.</p>
  
  <p>My code is below, but it is not correct.</p>
  
  <pre><code>static int getPeakCount(int[] arr, int R) {
          int result=0;
          for(int i=0;i&lt;arr.length;i++){
          if(i==0){
              if(arr[i]&gt;arr[i+1]&amp;&amp;arr[i]&gt;arr[i+2]){
                  result++;
              }
               } //-----&gt; closing if(i==0) condition
              else if(i==arr.length-1){
                  if(arr[i]&gt;arr[i-1]&amp;&amp;arr[i]&gt;arr[i-2]){
                      result++;
                  }
  
              }
              else if(i+R&gt;arr.length){
                  if(arr[i]&gt;arr[i-R] &amp;&amp; arr[i]&gt;arr[i-R+1]){
                      System.out.println(arr[i]);
                      result++;
                  }
              }
              else{
  
                  if(arr[i]&gt;arr[i+1] &amp;&amp; arr[i]&gt;arr[i+2] &amp;&amp; arr[i]&gt;arr[i-R] &amp;&amp; arr[i]&gt;arr[i-R+1]){
                      System.out.println(arr[i]);
                      result++;
              }
          }
      }
      return result;
  }
  </code></pre>
  
  <p>I don't know whether I'm going in the right direction or not, and for last if condition it's throwing an <code>java.lang.ArrayIndexOutOfBoundsException</code>.</p>
  
  <p>P.S. Don't consider this code as solution to remove errors from this. This is just the attempt I tried.</p>