Hi Friends

Today I attended Hackwithinfy round-2

in the second question(75 marks) :

problem statement: we will be having 4 arrays of n elements each

A, B, C, D and we have to find the maximum value of this equation

max(|A[i]-A[j]|+|B[i]-B[j]|+|C[i]-C[j]|+|D[i]-D[j]|+|i-j|) for 1<=i<j<=n

contrains :

2<=n<=10^5

Ip:

5

1 1 1 1 1

2 2 2 30 2

3 3 3 3 3

4 4 4 4 4

Op:

31

//explanation:

taking i=1 and j=4

i.e (1-1)+(30-2)+(3-3)+(4-4)+(4-1) => 31

my approach->

initially, I went for storing maximum and minimum array index for all the elements from right to left and spend O(1) time for every element in the array, do this same thing for all four arrays

later I realized that this is wrong

and also notices that for each test case they gave 5 SECONDS

so I went for brute forces

am I correct or should have optimized in any way please suggest