Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.

maxTriple([1, 2, 3]) → 3
maxTriple([1, 5, 3]) → 5
maxTriple([5, 2, 3]) → 5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public int maxTriple(int[] nums) {
  int max=0;
  int first=nums[0];
  int last=nums[nums.length-1];
  int mid=nums[nums.length/2];
  if ((first>last) && (first>mid)){
    max=first;
  }else if ((last>first) && (last>mid)) {
    max=last;
  }else{
    max=mid;
  }
  return max;
}

Keresés

Ajánló