-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAX_TRIPLE.java
More file actions
34 lines (28 loc) · 781 Bytes
/
MAX_TRIPLE.java
File metadata and controls
34 lines (28 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
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
*/
package school;
import java.util.Arrays;
public class MAX_TRIPLE {
public static void main(String[] args){
int[] nums = {1,2,3};
int answer = maxTriple(nums);
System.out.println(answer);
}
public static int maxTriple(int[] nums) {
int len = nums.length;
int half = len/2;
int a=0;
if(nums[0]>nums[len-1] && nums[0]>nums[half]){
return(nums[0]);
} else if(nums[len-1]>nums[0] && nums[len-1]>nums[half]){
return(nums[len-1]);
} else {
return nums[half];
}
}
}