Maximum value of |arr[i] – arr[j]| + |i – j|
Last Updated :
18 Sep, 2023
Given a array of N positive integers. The task is to find the maximum value of |arr[i] – arr[j]| + |i – j|, where 0 <= i, j <= N – 1 and arr[i], arr[j] belong to the array.
Examples:
Input : N = 4, arr[] = { 1, 2, 3, 1 }
Output : 4
Explanation:
Choose i = 0 and j = 2. This will result in |1-3|+|0-2| = 4 which is the maximum possible value.
Input : N = 3, arr[] = { 1, 1, 1 }
Output : 2
Method 1: The idea is to use brute force i.e iterate in two for loops.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 10
int findValue( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
ans = max(ans,
abs (arr[i] - arr[j]) + abs (i - j));
return ans;
}
int main()
{
int arr[] = { 1, 2, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findValue(arr, n) << endl;
return 0;
}
|
Java
class GFG {
static final int MAX = 10 ;
static int findValue( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
ans = Math.max(ans,
Math.abs(arr[i] - arr[j])
+ Math.abs(i - j));
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 1 };
int n = arr.length;
System.out.println(findValue(arr, n));
}
}
|
Python3
def findValue(arr, n):
ans = 0 ;
for i in range (n):
for j in range (n):
ans = ans if ans>( abs (arr[i] - arr[j]) +
abs (i - j)) else ( abs (arr[i] -
arr[j]) + abs (i - j)) ;
return ans;
arr = [ 1 , 2 , 3 , 1 ];
n = len (arr);
print (findValue(arr, n));
|
C#
PHP
Javascript
Method 2 (tricky): First of all lets make four equations by removing the absolute value signs (“|”). The following 4 equations will be formed, and we need to find the maximum value of these equations and that will be our answer.
- arr[i] – arr[j] + i – j = (arr[i] + i) – (arr[j] + j)
- arr[i] – arr[j] – i + j = (arr[i] – i) – (arr[j] – j)
- -arr[i] + arr[j] + i – j = -(arr[i] – i) + (arr[j] – j)
- -arr[i] + arr[j] – i + j = -(arr[i] + i) + (arr[j] + j)
Observe the equations (1) and (4) are identical. Similarly, equations (2) and (3) are identical.
Now the task is to find the maximum value of these equations. So the approach is to form two arrays, first_array[], it will store arr[i] + i, 0 <= i < n, second_array[], it will store arr[i] – i, 0 <= i < n.
Now our task is easy, we just need to find the maximum difference between the two values of these two arrays.
For that, we find the maximum value and minimum value in the first_array and store their difference:
ans1 = (maximum value in first_array – minimum value in first_array)
Similarly, we need to find the maximum value and minimum value in the second_array and store their difference:
ans2 = (maximum value in second_array – minimum value in second_array)
Our answer will be a maximum of ans1 and ans2.
Below is the implementation of the above approach:
C++
Java
Python3
C#
PHP
<?php
function findValue( $arr , $n )
{
$a [] = array (); $b = array (); $tmp ;
for ( $i = 0; $i < $n ; $i ++)
{
$a [ $i ] = ( $arr [ $i ] + $i );
$b [ $i ] = ( $arr [ $i ] - $i );
}
$x = $a [0]; $y = $a [0];
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > $x )
$x = $a [ $i ];
if ( $a [ $i ] < $y )
$y = $a [ $i ];
}
$ans1 = ( $x - $y );
$x = $b [0];
$y = $b [0];
for ( $i = 0; $i < $n ; $i ++)
{
if ( $b [ $i ] > $x )
$x = $b [ $i ];
if ( $b [ $i ] < $y )
$y = $b [ $i ];
}
$ans2 = ( $x - $y );
return max( $ans1 , $ans2 );
}
$arr = array (1, 2, 3, 1);
$n = count ( $arr );
echo findValue( $arr , $n );
?>
|
Javascript
<script>
function findValue(arr, n)
{
let a = new Array(n);
let b = new Array(n);
for (let i = 0; i < n; i++)
{
a[i] = (arr[i] + i);
b[i] = (arr[i] - i);
}
let x = a[0], y = a[0];
for (let i = 0; i < n; i++)
{
if (a[i] > x)
x = a[i];
if (a[i] < y)
y = a[i];
}
let ans1 = (x - y);
x = b[0];
y = b[0];
for (let i = 0; i < n; i++)
{
if (b[i] > x)
x = b[i];
if (b[i] < y)
y = b[i];
}
let ans2 = (x - y);
return Math.max(ans1, ans2);
}
let arr = [ 1, 2, 3, 1 ];
let n = arr.length;
document.write(findValue(arr, n));
</script>
|
Method – 3
This solution is space optimization on above mentioned (method2) solution.
In Method 2 solution we had used two matrix of size n which laid to O(n) space complexity
but here we only use O(1) space instead of that two n size array
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findValue( int A[], int n)
{
int mx1=INT_MIN;
int mn1=INT_MAX;
int mn2=INT_MAX;
int mx2=INT_MIN;
for ( int i=0;i<n;i++){
mx1=max(mx1,A[i]+i);
mn1=min(mn1,A[i]+i);
mx2=max(mx2,A[i]-i);
mn2=min(mn2,A[i]-i);
}
int res=0;
res=max(res,mx1-mn1);
res=max(res,mx2-mn2);
return res;
}
int main()
{
int arr[] = { 1, 2, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findValue(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findValue( int arr[], int n)
{
int temp1, temp2;
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
temp1 = arr[i] + i;
temp2 = arr[i] - i;
max1 = Math.max(max1, temp1);
min1 = Math.min(min1, temp1);
max2 = Math.max(max2, temp2);
min2 = Math.min(min2, temp2);
}
return Math.max((max1 - min1), (max2 - min2));
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 1 };
int n = arr.length;
System.out.print(findValue(arr, n) + "\n" );
}
}
|
Python3
import sys
def findValue(arr, n):
temp1 = 0 ;
temp2 = 0 ;
max1 = - sys.maxsize;
max2 = - sys.maxsize;
min1 = sys.maxsize;
min2 = sys.maxsize;
for i in range (n):
temp1 = arr[i] + i;
temp2 = arr[i] - i;
max1 = max (max1, temp1);
min1 = min (min1, temp1);
max2 = max (max2, temp2);
min2 = min (min2, temp2);
return max ((max1 - min1), (max2 - min2));
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 1 ];
n = len (arr);
print (findValue(arr, n));
|
C#
using System;
public class GFG {
static int findValue( int [] arr, int n)
{
int temp1, temp2;
int max1 = int .MinValue, max2 = int .MinValue;
int min1 = int .MaxValue, min2 = int .MaxValue;
for ( int i = 0; i < n; i++) {
temp1 = arr[i] + i;
temp2 = arr[i] - i;
max1 = Math.Max(max1, temp1);
min1 = Math.Min(min1, temp1);
max2 = Math.Max(max2, temp2);
min2 = Math.Min(min2, temp2);
}
return Math.Max((max1 - min1), (max2 - min2));
}
public static void Main(String[] args)
{
int [] arr = { 1, 2, 3, 1 };
int n = arr.Length;
Console.Write(findValue(arr, n) + "\n" );
}
}
|
Javascript
<script>
function findValue(arr , n) {
var temp1, temp2;
var max1 = Number.MIN_VALUE, max2 = Number.MIN_VALUE;
var min1 = Number.MAX_VALUE, min2 = Number.MAX_VALUE;
for (i = 0; i < n; i++) {
temp1 = arr[i] + i;
temp2 = arr[i] - i;
max1 = Math.max(max1, temp1);
min1 = Math.min(min1, temp1);
max2 = Math.max(max2, temp2);
min2 = Math.min(min2, temp2);
}
return Math.max((max1 - min1), (max2 - min2));
}
var arr = [ 1, 2, 3, 1 ];
var n = arr.length;
document.write(findValue(arr, n) + "\n" );
</script>
|
Time Complexity : O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...