Maximum value of |arr[i] – arr[j]| + |i – j|

Given a array of N positive integers. The task is to find 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
Choose i = 0 and j = 4

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
#define MAX 10
  
// Return maximum value of |arr[i] - arr[j]| + |i - j|
int findValue(int arr[], int n)
{
    int ans = 0;
  
    // Iterating two for loop, one for i and another for j.
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < n; j++) 
  
            // Evaluating |arr[i] - arr[j]| + |i - j| 
            // and compare with previous maximum.
            ans = max(ans, abs(arr[i] - arr[j]) + abs(i - j));
  
    return ans;
}
  
// Driven Program
int main()
{
    int arr[] = { 1, 2, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << findValue(arr, n) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to find maximum value of 
// |arr[i] - arr[j]| + |i - j|
class GFG
{
    static final int MAX = 10;
      
    // Return maximum value of 
    // |arr[i] - arr[j]| + |i - j|
    static int findValue(int arr[], int n)
    {
        int ans = 0;
      
        // Iterating two for loop, 
        // one for i and another for j.
        for (int i = 0; i < n; i++) 
            for (int j = 0; j < n; j++) 
      
                // Evaluating |arr[i] - arr[j]|
                // + |i - j| and compare with
                // previous maximum.
                ans = Math.max(ans, 
                     Math.abs(arr[i] - arr[j]) 
                            + Math.abs(i - j));
      
        return ans;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 1, 2, 3, 1 };
        int n =arr.length;
          
        System.out.println(findValue(arr, n));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# maximum value of 
# |arr[i] - arr[j]| + |i - j|
  
# Return maximum value of
# |arr[i] - arr[j]| + |i - j|
def findValue(arr, n):
    ans = 0;
      
    # Iterating two for loop, 
    # one for i and another for j.
    for i in range(n):
        for j in range(n):
              
            # Evaluating |arr[i] -
            # arr[j]| + |i - j|
            # and compare with
            # previous maximum.
            ans = ans if ans>(abs(arr[i] - arr[j]) + 
                              abs(i - j)) else (abs(arr[i] -
                                      arr[j]) + abs(i - j)) ;
    return ans;
      
# Driver Code
arr = [1, 2, 3, 1];
n = len(arr);
print(findValue(arr, n));
  
# This code is contributed by mits.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum value of 
// |arr[i] - arr[j]| + |i - j|
using System;
  
class GFG {
      
    // Return maximum value of 
    // |arr[i] - arr[j]| + |i - j|
    static int findValue(int []arr, int n)
    {
        int ans = 0;
      
        // Iterating two for loop, 
        // one for i and another for j.
        for (int i = 0; i < n; i++) 
            for (int j = 0; j < n; j++) 
      
                // Evaluating |arr[i] - arr[j]|
                // + |i - j| and compare with
                // previous maximum.
                ans = Math.Max(ans, 
                    Math.Abs(arr[i] - arr[j]) 
                            + Math.Abs(i - j));
      
        return ans;
    }
      
    // Driver code
    public static void Main ()
    {
        int []arr = { 1, 2, 3, 1 };
        int n =arr.Length;
          
        Console.Write(findValue(arr, n));
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find maximum value of 
// |arr[i] - arr[j]| + |i - j|
$MAX = 10;
  
// Return maximum value of
// |arr[i] - arr[j]| + |i - j|
function findValue($arr, $n)
{
    $ans = 0;
  
    // Iterating two for loop, 
    // one for i and another for j.
    for ($i = 0; $i < $n; $i++) 
        for ($j = 0; $j < $n; $j++) 
  
            // Evaluating |arr[i] - 
            // arr[j]| + |i - j| 
            // and compare with
            // previous maximum.
            $ans = max($ans, abs($arr[$i] - 
                   $arr[$j]) + abs($i - $j));
  
    return $ans;
}
      
    // Driver Code
    $arr = array(1, 2, 3, 1);
    $n = count($arr);
  
    echo findValue($arr, $n);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output:

4

Method 2 (tricky):
First of all lets make four equations by removing the absolute value signs (“|”). Following 4 equations will be formed, and we need to find the maximum value of these equation and that will be our answer.

  1. arr[i] – arr[j] + i – j = (arr[i] + i) – (arr[j] + j)
  2. arr[i] – arr[j] – i + j = (arr[i] – i) – (arr[j] – j)
  3. -arr[i] + arr[j] + i – j = -(arr[i] – i) + (arr[j] – j)
  4. -arr[i] + arr[j] – i + j = -(arr[i] + i) + (arr[j] + j)

Observe equation (1) and (4) are identical, similarly equation (2) and (3) are identical.

Now the task is to find the maximum value of these equation. 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 two values these two arrays.

For that, we find maximum value and minimum value in first_array and store their difference:
ans1 = (maximum value in first_array – minimum value in first_array)
Similarly, we need to find maximum value and minimum value in second_array and store their difference:
ans2 = (maximum value in second_array – minimum value in second_array)
Our answer will be maximum of ans1 and ans2.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient CPP program to find maximum value
// of |arr[i] - arr[j]| + |i - j|
#include <bits/stdc++.h>
using namespace std;
  
// Return maximum |arr[i] - arr[j]| + |i - j|
int findValue(int arr[], int n)
{
    int a[n], b[n], tmp;
  
    // Calculating first_array and second_array
    for (int i = 0; i < n; i++) {
        a[i] = (arr[i] + i);
        b[i] = (arr[i] - i);
    }
  
    int x = a[0], y = a[0];
  
    // Finding maximum and minimum value in 
    // first_array
    for (int i = 0; i < n; i++) {
        if (a[i] > x)
            x = a[i];
  
        if (a[i] < y)
            y = a[i];
    }
  
    // Storing the difference between maximum and 
    // minimum value in first_array
    int ans1 = (x - y);
  
    x = b[0];
    y = b[0];
  
    // Finding maximum and minimum value in
    // second_array
    for (int i = 0; i < n; i++) {
        if (b[i] > x)
            x = b[i];
  
        if (b[i] < y)
            y = b[i];
    }
  
    // Storing the difference between maximum and 
    // minimum value in second_array
    int ans2 = (x - y);
  
    return max(ans1, ans2);
}
  
// Driven Program
int main()
{
    int arr[] = { 1, 2, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << findValue(arr, n) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient Java program to find maximum
// value of |arr[i] - arr[j]| + |i - j|
import java.io.*;
class GFG {
  
// Return maximum |arr[i] -
// arr[j]| + |i - j|
static int findValue(int arr[], int n)
{
    int a[] = new int[n]; 
    int b[] = new int[n];
    int tmp;
  
    // Calculating first_array
    // and second_array
    for (int i = 0; i < n; i++)
    {
        a[i] = (arr[i] + i);
        b[i] = (arr[i] - i);
    }
  
    int x = a[0], y = a[0];
  
    // Finding maximum and 
    // minimum value in 
    // first_array
    for (int i = 0; i < n; i++) 
    {
        if (a[i] > x)
            x = a[i];
  
        if (a[i] < y)
            y = a[i];
    }
  
    // Storing the difference 
    // between maximum and 
    // minimum value in first_array
    int ans1 = (x - y);
  
    x = b[0];
    y = b[0];
  
    // Finding maximum and 
    // minimum value in
    // second_array
    for (int i = 0; i < n; i++)
    {
        if (b[i] > x)
            x = b[i];
  
        if (b[i] < y)
            y = b[i];
    }
  
    // Storing the difference
    // between maximum and 
    // minimum value in second_array
    int ans2 = (x - y);
  
    return Math.max(ans1, ans2);
}
  
    // Driver Code
    public static void main(String[] args) 
    {
        int arr[] = {1, 2, 3, 1};
        int n = arr.length;
        System.out.println( findValue(arr, n));
    }
}
  
// This code is contributed by anuj_67.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Efficient Python3 program 
# to find maximum value
# of |arr[i] - arr[j]| + |i - j|
  
# Return maximum |arr[i] - 
# arr[j]| + |i - j|
def findValue(arr, n):
    a=[]
    b=[]
  
    # Calculating first_array 
    # and second_array
    for i in range(n):
        a.append(arr[i] + i)
        b.append(arr[i] - i)
  
    x = a[0
    y = a[0]
  
    # Finding maximum and 
    # minimum value in 
    # first_array
    for i in range(n):
        if (a[i] > x):
            x = a[i]
  
        if (a[i] < y):
            y = a[i]
  
    # Storing the difference 
    # between maximum and 
    # minimum value in first_array
    ans1 = (x - y)
  
    x = b[0]
    y = b[0]
  
    # Finding maximum and 
    # minimum value in
    # second_array
    for i in range(n):
        if (b[i] > x):
            x = b[i]
  
        if (b[i] < y):
            y = b[i]
  
    # Storing the difference 
    # between maximum and 
    # minimum value in 
    # second_array
    ans2 = (x -y)
  
    return max(ans1, ans2)
  
# Driver Code
if __name__=='__main__':
    arr = [ 1, 2, 3, 1]
    n = len(arr)
  
    print(findValue(arr, n))
      
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient Java program to find maximum
// value of |arr[i] - arr[j]| + |i - j|
using System;
class GFG {
  
// Return maximum |arr[i] -
// arr[j]| + |i - j|
static int findValue(int []arr, int n)
{
    int []a = new int[n]; 
    int []b = new int[n];
    //int tmp;
  
    // Calculating first_array
    // and second_array
    for (int i = 0; i < n; i++)
    {
        a[i] = (arr[i] + i);
        b[i] = (arr[i] - i);
    }
  
    int x = a[0], y = a[0];
  
    // Finding maximum and 
    // minimum value in 
    // first_array
    for (int i = 0; i < n; i++) 
    {
        if (a[i] > x)
            x = a[i];
  
        if (a[i] < y)
            y = a[i];
    }
  
    // Storing the difference 
    // between maximum and 
    // minimum value in first_array
    int ans1 = (x - y);
  
    x = b[0];
    y = b[0];
  
    // Finding maximum and 
    // minimum value in
    // second_array
    for (int i = 0; i < n; i++)
    {
        if (b[i] > x)
            x = b[i];
  
        if (b[i] < y)
            y = b[i];
    }
  
    // Storing the difference
    // between maximum and 
    // minimum value in second_array
    int ans2 = (x - y);
  
    return Math.Max(ans1, ans2);
}
  
    // Driver Code
    public static void Main() 
    {
        int []arr = {1, 2, 3, 1};
        int n = arr.Length;
        Console.WriteLine( findValue(arr, n));
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Efficient CPP program 
// to find maximum value
// of |arr[i] - arr[j]| + |i - j|
  
// Return maximum |arr[i] - 
// arr[j]| + |i - j|
function findValue($arr, $n)
{
    $a[] =array(); $b=array();$tmp;
  
    // Calculating first_array 
    // and second_array
    for ($i = 0; $i < $n; $i++)
    {
        $a[$i] = ($arr[$i] + $i);
        $b[$i] = ($arr[$i] - $i);
    }
  
    $x = $a[0]; $y = $a[0];
  
    // Finding maximum and 
    // minimum value in 
    // first_array
    for ($i = 0; $i < $n; $i++)
    {
        if ($a[$i] > $x)
        $x = $a[$i];
  
        if ($a[$i] < $y)
            $y = $a[$i];
    }
  
    // Storing the difference 
    // between maximum and 
    // minimum value in first_array
    $ans1 = ($x - $y);
  
    $x = $b[0];
    $y = $b[0];
  
    // Finding maximum and 
    // minimum value in
    // second_array
    for ($i = 0; $i < $n; $i++)
    {
        if ($b[$i] > $x)
            $x = $b[$i];
  
        if ($b[$i] < $y)
            $y = $b[$i];
    }
  
    // Storing the difference 
    // between maximum and 
    // minimum value in 
    // second_array
    $ans2 = ($x -$y);
  
    return max($ans1, $ans2);
}
  
    // Driver Code
    $arr = array(1, 2, 3, 1);
    $n = count($arr);
  
    echo findValue($arr, $n);
      
// This code is contributed by anuj_67.
?>

chevron_right


Output:

4

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.