Maximum absolute difference of value and index sums
Last Updated :
29 Jul, 2022
Given an unsorted array A of N integers, Return maximum value of f(i, j) for all 1 ? i, j ? N.
f(i, j) or absolute difference of two elements of an array A is defined as |A[i] – A[j]| + |i – j|, where |A| denotes the absolute value of A.
Examples:
We will calculate the value of f(i, j) for each pair
of (i, j) and return the maximum value thus obtained.
Input : A = {1, 3, -1}
Output : 5
f(1, 1) = f(2, 2) = f(3, 3) = 0
f(1, 2) = f(2, 1) = |1 - 3| + |1 - 2| = 3
f(1, 3) = f(3, 1) = |1 - (-1)| + |1 - 3| = 4
f(2, 3) = f(3, 2) = |3 - (-1)| + |2 - 3| = 5
So, we return 5.
Input : A = {3, -2, 5, -4}
Output : 10
f(1, 1) = f(2, 2) = f(3, 3) = f(4, 4) = 0
f(1, 2) = f(2, 1) = |3 - (-2)| + |1 - 2| = 6
f(1, 3) = f(3, 1) = |3 - 5| + |1 - 3| = 4
f(1, 4) = f(4, 1) = |3 - (-4)| + |1 - 4| = 10
f(2, 3) = f(3, 2) = |(-2) - 5| + |2 - 3| = 8
f(2, 4) = f(4, 2) = |(-2) - (-4)| + |2 - 4| = 4
f(3, 4) = f(4, 3) = |5 - (-4)| + |3 - 4| = 10
So, we return 10
A naive brute force approach is to calculate the value f(i, j) by iterating over all such pairs (i, j) and calculating the maximum absolute difference which is implemented below.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int calculateDiff( int i, int j, int arr[])
{
return abs (arr[i] - arr[j]) + abs (i - j);
}
int maxDistance( int arr[], int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
if (calculateDiff(i, j, arr) > result)
result = calculateDiff(i, j, arr);
}
}
return result;
}
int main()
{
int arr[] = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxDistance(arr, n) << endl;
return 0;
}
|
Java
public class MaximumAbsoluteDifference
{
private static int calculateDiff( int i, int j,
int [] array)
{
return Math.abs(array[i] - array[j]) +
Math.abs(i - j);
}
private static int maxDistance( int [] array)
{
int result = 0 ;
for ( int i = 0 ; i < array.length; i++)
{
for ( int j = i; j < array.length; j++)
{
result = Math.max(result, calculateDiff(i, j, array));
}
}
return result;
}
public static void main(String[] args)
{
int [] array = { - 70 , - 64 , - 6 , - 56 , 64 ,
61 , - 57 , 16 , 48 , - 98 };
System.out.println(maxDistance(array));
}
}
|
Python3
def calculateDiff(i, j, arr):
return abs (arr[i] - arr[j]) + abs (i - j)
def maxDistance(arr, n):
result = 0
for i in range ( 0 ,n):
for j in range (i, n):
if (calculateDiff(i, j, arr) > result):
result = calculateDiff(i, j, arr)
return result
arr = [ - 70 , - 64 , - 6 , - 56 , 64 ,
61 , - 57 , 16 , 48 , - 98 ]
n = len (arr)
print (maxDistance(arr, n))
|
C#
using System;
public class MaximumAbsoluteDifference
{
private static int calculateDiff( int i, int j,
int [] array)
{
return Math.Abs(array[i] - array[j]) +
Math.Abs(i - j);
}
private static int maxDistance( int [] array)
{
int result = 0;
for ( int i = 0; i < array.Length; i++)
{
for ( int j = i; j < array.Length; j++)
{
result = Math.Max(result, calculateDiff(i, j, array));
}
}
return result;
}
public static void Main()
{
int [] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
Console.WriteLine(maxDistance(array));
}
}
|
PHP
<?php
function calculateDiff( $i , $j , $arr )
{
return abs ( $arr [ $i ] - $arr [ $j ]) +
abs ( $i - $j );
}
function maxDistance( $arr , $n )
{
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i ; $j < $n ; $j ++)
{
if (calculateDiff( $i , $j , $arr ) > $result )
$result = calculateDiff( $i , $j , $arr );
}
}
return $result ;
}
$arr = array ( -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 );
$n = sizeof( $arr );
echo maxDistance( $arr , $n );
?>
|
Javascript
<script>
let MAX = 256;
function calculateDiff(i, j, array)
{
return Math.abs(array[i] - array[j]) +
Math.abs(i - j);
}
function maxDistance(array)
{
let result = 0;
for (let i = 0; i < array.length; i++)
{
for (let j = i; j < array.length; j++)
{
result = Math.max(result, calculateDiff(i, j, array));
}
}
return result;
}
let array = [-70, -64, -6, -56, 64,
61, -57, 16, 48, -98 ];
document.write(maxDistance(array));
</script>
|
Time complexity: O(n2)
Auxiliary Space: O(1)
An efficient solution in O(n) time complexity can be worked out using the properties of absolute values.
f(i, j) = |A[i] – A[j]| + |i – j| can be written in 4 ways (Since we are looking at max value, we don’t even care if the value becomes negative as long as we are also covering the max value in some way).
Case 1: A[i] > A[j] and i > j
|A[i] - A[j]| = A[i] - A[j]
|i -j| = i - j
hence, f(i, j) = (A[i] + i) - (A[j] + j)
Case 2: A[i] < A[j] and i < j
|A[i] - A[j]| = -(A[i]) + A[j]
|i -j| = -(i) + j
hence, f(i, j) = -(A[i] + i) + (A[j] + j)
Case 3: A[i] > A[j] and i < j
|A[i] - A[j]| = A[i] - A[j]
|i -j| = -(i) + j
hence, f(i, j) = (A[i] - i) - (A[j] - j)
Case 4: A[i] < A[j] and i > j
|A[i] - A[j]| = -(A[i]) + A[j]
|i -j| = i - j
hence, f(i, j) = -(A[i] - i) + (A[j] - j)
Note that cases 1 and 2 are equivalent and so are cases 3 and 4 and hence we can design our algorithm only for two cases as it will cover all the possible cases.
1. Calculate the value of A[i] + i and A[i] – i for every element of the array while traversing through the array.
2. Then for the two equivalent cases, we find the maximum possible value. For that, we have to store minimum and maximum values of expressions A[i] + i and A[i] – i for all i.
3. Hence the required maximum absolute difference is maximum of two values i.e. max((A[i] + i) – (A[j] + j)) and max((A[i] – i) – (A[j] – j)). These values can be found easily in linear time.
a. For max((A[i] + i) – (A[j] + j)) Maintain two variables max1 and min1 which will store maximum and minimum values of A[i] + i respectively. max((A[i] + i) – (A[j] + j)) = max1 – min1
b. For max((A[i] – i) – (A[j] – j)). Maintain two variables max2 and min2 which will store maximum and minimum values of A[i] – i respectively. max((A[i] – i) – (A[j] – j)) = max2 – min2
Implementation using the above fast algorithm is given below.
C++
#include <bits/stdc++.h>
using namespace std;
int maxDistance( int arr[], int n)
{
int max1 = INT_MIN, min1 = INT_MAX;
int max2 = INT_MIN, min2 = INT_MAX;
for ( int i = 0; i < n; i++) {
max1 = max(max1, arr[i] + i);
min1 = min(min1, arr[i] + i);
max2 = max(max2, arr[i] - i);
min2 = min(min2, arr[i] - i);
}
return max(max1 - min1, max2 - min2);
}
int main()
{
int arr[] = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxDistance(arr, n) << endl;
return 0;
}
|
Java
public class MaximumAbsoluteDifference
{
private static int maxDistance( int [] array)
{
int max1 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE;
int max2 = Integer.MIN_VALUE;
int min2 = Integer.MAX_VALUE;
for ( int i = 0 ; i < array.length; i++)
{
max1 = Math.max(max1, array[i] + i);
min1 = Math.min(min1, array[i] + i);
max2 = Math.max(max2, array[i] - i);
min2 = Math.min(min2, array[i] - i);
}
return Math.max(max1 - min1, max2 - min2);
}
public static void main(String[] args)
{
int [] array = { - 70 , - 64 , - 6 , - 56 , 64 ,
61 , - 57 , 16 , 48 , - 98 };
System.out.println(maxDistance(array));
}
}
|
Python3
def maxDistance(array):
max1 = - 2147483648
min1 = + 2147483647
max2 = - 2147483648
min2 = + 2147483647
for i in range ( len (array)):
max1 = max (max1, array[i] + i)
min1 = min (min1, array[i] + i)
max2 = max (max2, array[i] - i)
min2 = min (min2, array[i] - i)
return max (max1 - min1, max2 - min2)
array = [ - 70 , - 64 , - 6 , - 56 , 64 ,
61 , - 57 , 16 , 48 , - 98 ]
print (maxDistance(array))
|
C#
using System;
public class MaximumAbsoluteDifference
{
private static int maxDistance( int [] array)
{
int max1 = int .MinValue ;
int min1 = int .MaxValue ;
int max2 = int .MinValue ;
int min2 = int .MaxValue ;
for ( int i = 0; i < array.Length; i++)
{
max1 = Math.Max(max1, array[i] + i);
min1 = Math.Min(min1, array[i] + i);
max2 = Math.Max(max2, array[i] - i);
min2 = Math.Min(min2, array[i] - i);
}
return Math.Max(max1 - min1, max2 - min2);
}
public static void Main()
{
int [] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
Console.WriteLine(maxDistance(array));
}
}
|
PHP
<?php
function maxDistance( $arr , $n )
{
$max1 = PHP_INT_MIN; $min1 =
PHP_INT_MAX;
$max2 = PHP_INT_MIN; $min2 =
PHP_INT_MAX;
for ( $i = 0; $i < $n ; $i ++)
{
$max1 = max( $max1 , $arr [ $i ] + $i );
$min1 = min( $min1 , $arr [ $i ] + $i );
$max2 = max( $max2 , $arr [ $i ] - $i );
$min2 = min( $min2 , $arr [ $i ] - $i );
}
return max( $max1 - $min1 ,
$max2 - $min2 );
}
$arr = array (-70, -64, -6, -56, 64,
61, -57, 16, 48, -98);
$n = count ( $arr );
echo maxDistance( $arr , $n );
?>
|
Javascript
<script>
function maxDistance(array)
{
let max1 = Number.MIN_VALUE;
let min1 = Number.MAX_VALUE;
let max2 = Number.MIN_VALUE;
let min2 = Number.MAX_VALUE;
for (let i = 0; i < array.length; i++)
{
max1 = Math.max(max1, array[i] + i);
min1 = Math.min(min1, array[i] + i);
max2 = Math.max(max2, array[i] - i);
min2 = Math.min(min2, array[i] - i);
}
return Math.max(max1 - min1, max2 - min2);
}
let array =
[ -70, -64, -6, -56, 64, 61, -57, 16, 48, -98 ];
document.write(maxDistance(array));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...