1
+ // Runtime: 15 ms (Top 33.52%) | Memory: 58.1 MB (Top 8.52%)
2
+ class Solution {
3
+ public int maxAbsValExpr (int [] arr1 , int [] arr2 ) {
4
+
5
+ //1. remove the modulas -
6
+ //i & j are interchangable because they are inside the modulas
7
+ // A[i] - A[j] + B[i] -B[j] + i-j
8
+ // A[i] + B[i] + i - B[j] - A[j] - j
9
+ // (A[i] + B[i] + i) ->X
10
+ // (B[j] - A[j] - j) -> y
11
+ // X - Y;
12
+ //to get max value X should be max & Y should min
13
+
14
+ // Possible cases (Since both arrays have same number of indexes, we can use single for loop & i as index)
15
+ //A[i] + B[i] + i ->1
16
+ //A[i] - B[i] + i ->2
17
+ //A[i] + B[i] - i ->3
18
+ //A[i] - B[i] - i ->4
19
+
20
+ // Find out max of all response
21
+
22
+ int arrayLength =arr1 .length ;
23
+ int v1 [] = new int [arrayLength ];
24
+ int v2 [] = new int [arrayLength ] ;
25
+ int v3 [] = new int [arrayLength ] ;
26
+ int v4 [] = new int [arrayLength ] ;
27
+ int res = 0 ;
28
+ for (int i = 0 ; i < arrayLength ; i ++)
29
+ {
30
+ v1 [i ] = i + arr1 [i ] + arr2 [i ];
31
+ v2 [i ] = i + arr1 [i ] - arr2 [i ];
32
+ v3 [i ] = i - arr1 [i ] + arr2 [i ];
33
+ v4 [i ] = i - arr1 [i ] - arr2 [i ];
34
+ }
35
+ res = Math .max (res ,Arrays .stream (v1 ).max ().getAsInt ()-Arrays .stream (v1 ).min ().getAsInt ());
36
+ res = Math .max (res ,Arrays .stream (v2 ).max ().getAsInt ()-Arrays .stream (v2 ).min ().getAsInt ());
37
+ res = Math .max (res ,Arrays .stream (v3 ).max ().getAsInt ()-Arrays .stream (v3 ).min ().getAsInt ());
38
+ res = Math .max (res ,Arrays .stream (v4 ).max ().getAsInt ()-Arrays .stream (v4 ).min ().getAsInt ());
39
+ return res ;
40
+ }
41
+ }
0 commit comments