//       CS210 - Algorithms and Data Structures I
//            Department of Computer Science
//       National University of Ireland, Maynooth
//
//           Solutions to Self-help Exercises
//                     Tom Naughton
//                    November 2000
//
//-------------------------------------------------------
//http://www.cs.may.ie/tnaughton/cs210/cs210selfhelp.html
//=======================================================
class ExerciseII18 {

  public static int fn(int list[]) {
    /* This function returns the index of the largest int in list.
    ** If there are multiple occurances of this int we return the
    ** highest index. If the list has no elements, return -1.
    */
    int count;   // counter
    int highest; // highest index

    if (list == null) {
      return -1;

    } else if (list.length == 0) {
      return -1;

    } else {
      highest = 0;
      count = 1; // start at second element (if it exists)
      while (count < list.length) {
        if (list[count] >= list[highest]) {
          highest = count;
        }
        count++;
      }
      return highest;
    }
  }

  public static void main(String args[]) {
    int a[] = {1, 5, 7, 4, 3, 7, 6};
    System.out.println("The largest value is at index " + fn(a) + '.');
  }
}

