You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.6 KiB
64 lines
1.6 KiB
package class23;
|
|
|
|
public class Code02_MaxABSBetweenLeftAndRight {
|
|
|
|
public static int maxABS1(int[] arr) {
|
|
int res = Integer.MIN_VALUE;
|
|
int maxLeft = 0;
|
|
int maxRight = 0;
|
|
for (int i = 0; i != arr.length - 1; i++) {
|
|
maxLeft = Integer.MIN_VALUE;
|
|
for (int j = 0; j != i + 1; j++) {
|
|
maxLeft = Math.max(arr[j], maxLeft);
|
|
}
|
|
maxRight = Integer.MIN_VALUE;
|
|
for (int j = i + 1; j != arr.length; j++) {
|
|
maxRight = Math.max(arr[j], maxRight);
|
|
}
|
|
res = Math.max(Math.abs(maxLeft - maxRight), res);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public static int maxABS2(int[] arr) {
|
|
int[] lArr = new int[arr.length];
|
|
int[] rArr = new int[arr.length];
|
|
lArr[0] = arr[0];
|
|
rArr[arr.length - 1] = arr[arr.length - 1];
|
|
for (int i = 1; i < arr.length; i++) {
|
|
lArr[i] = Math.max(lArr[i - 1], arr[i]);
|
|
}
|
|
for (int i = arr.length - 2; i > -1; i--) {
|
|
rArr[i] = Math.max(rArr[i + 1], arr[i]);
|
|
}
|
|
int max = 0;
|
|
for (int i = 0; i < arr.length - 1; i++) {
|
|
max = Math.max(max, Math.abs(lArr[i] - rArr[i + 1]));
|
|
}
|
|
return max;
|
|
}
|
|
|
|
public static int maxABS3(int[] arr) {
|
|
int max = Integer.MIN_VALUE;
|
|
for (int i = 0; i < arr.length; i++) {
|
|
max = Math.max(arr[i], max);
|
|
}
|
|
return max - Math.min(arr[0], arr[arr.length - 1]);
|
|
}
|
|
|
|
public static int[] generateRandomArray(int length) {
|
|
int[] arr = new int[length];
|
|
for (int i = 0; i != arr.length; i++) {
|
|
arr[i] = (int) (Math.random() * 1000) - 499;
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int[] arr = generateRandomArray(200);
|
|
System.out.println(maxABS1(arr));
|
|
System.out.println(maxABS2(arr));
|
|
System.out.println(maxABS3(arr));
|
|
}
|
|
}
|