title: Find index of max element
body: <p>I am having trouble getting the index where the greatest element is found. I know elements in the array can be accessed by X[r], where r is the index and that is what I did here, but I can not seem to get it get the index only. </p>
  
  <p>Code: </p>
  
  <pre><code>public class Max {
  
  public static void main(String[] args) {
      int[] B = {-1, 2, 6, 3, 9, 2, -3, -2, 11, 5, 7};
      System.out.println("max = " + maxArrayIndex(B, 0, B.length-1));
  }
  
  static int maxArrayIndex(int[] X, int p, int r) {
      int q = 0;
      if(p &lt; r) {
         q = (p + r)/2;
         int maxLeft = maxArrayIndex(X, p, q);
         int maxRight = maxArrayIndex(X, q+1, r);
         int maxFinal = max(maxLeft, maxRight);
         return maxFinal;
        }
        return X[r];
     }
  
     static int max(int p , int r) {
         int maxIndex = 0;
         if(p &gt; r) {
            maxIndex = p;
         } else {
             maxIndex = r;
         }
         return maxIndex;
       }
  }
  </code></pre>