Given two arrays of integers with equal lengths, return the maximum value of:
|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|
where the maximum is taken over all 0 <= i, j < arr1.length.
Example 1: Input: arr1 = [1,2,3,4], arr2 = [-1,4,5,6] Output: 13
Example 2: Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4] Output: 20
Constraints: 2 <= arr1.length == arr2.length <= 40000 -10^6 <= arr1[i], arr2[i] <= 10^6
public class Solution {
public int MaxAbsValExpr(int[] arr1, int[] arr2) {
int max = 0;
for (int i =0; i < arr1.Length; i++){
for (int k =i+1; k < arr1.Length; k++){
if (Math.Abs(arr1[i] - arr1[k]) + Math.Abs(arr2[i] - arr2[k]) + Math.Abs(i - k) > max){
max = Math.Abs(arr1[i] - arr1[k]) + Math.Abs(arr2[i] - arr2[k]) + Math.Abs(i - k);
}
}
}
return max;
}}
This passes most test cases but gets a "Time Limit Exceeded" when the test case has hundreds of values in the arrays in the six digits. I've tried looking online to see how to shorten it in c# but I don't know what else I could do to make it faster.