Maximize value of (arr[i] – i) – (arr[j] – j) in an array

Given an array, arr[] find the maximum value of (arr[i] – i) – (arr[j] – j) where i is not equal to j. Where i and j vary from 0 to n-1 and n is size of input array arr[].

Examples:

Input : arr[] = {9, 15, 4, 12, 13}
Output : 12
We get the maximum value for i = 1 and j = 2
(15 - 1) - (4 - 2) = 12

Input : arr[] = {-1, -2, -3, 4, 10}
Output : 6
We get the maximum value for i = 4 and j = 2
(10 - 4) - (-3 - 2) = 11

One important observation is, value of (arr[i] – i) – (arr[j] – j) can never be negative. We can alway swap i and j to convert a negative value into positive. So the condition i not equal to j is bogus and doesn’t require explicit check.


Method 1 (Naive : O(n2))

The idea is to run two loops to consider all possible pairs and keep track of maximum value of expression (arr[i]-i)-(arr[j]-j). Below is the implementation of this idea.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
#include<bits/stdc++.h>
using namespace std;
  
// Returns maximum value of (arr[i]-i) - (arr[j]-j)
int findMaxDiff(int arr[], int n)
{
    if (n < 2)
    {
        cout << "Invalid ";
        return 0;
    }
  
    // Use two nested loops to find the result
    int res = INT_MIN;
    for (int i=0; i<n; i++)
       for (int j=0; j<n; j++)
          if ( res < (arr[i]-arr[j]-i+j) )
            res = (arr[i]-arr[j]-i+j);
  
    return res;
}
  
// Driver program
int main()
{
   int arr[] = {9, 15, 4, 12, 13};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout << findMaxDiff(arr, n);
   return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
import java.util.*;
  
class GFG {
      
// Returns maximum value of
// (arr[i]-i) - (arr[j]-j)
static int findMaxDiff(int arr[], int n) 
{
    if (n < 2) {
    System.out.print("Invalid ");
    return 0;
    }
  
    // Use two nested loops to find the result
    int res = Integer.MIN_VALUE;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        if (res < (arr[i] - arr[j] - i + j))
        res = (arr[i] - arr[j] - i + j);
  
    return res;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = {9, 15, 4, 12, 13};
    int n = arr.length;
    System.out.print(findMaxDiff(arr, n));
}
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find
# maximum value (arr[i]-i)
# - (arr[j]-j) in an array.
  
# Returns maximum value of
# (arr[i]-i) - (arr[j]-j)
def findMaxDiff(arr,n):
  
    if (n < 2):
      
        print("Invalid ")
        return 0
      
   
    # Use two nested loops
    # to find the result
    res = -2147483648
    for i in range(n):
        for j in range(n):
            if ( res < (arr[i]-arr[j]-i+j) ):
                res = (arr[i]-arr[j]-i+j)
   
    return res
  
# Driver code
  
arr= [9, 15, 4, 12, 13]
n = len(arr)
  
print(findMaxDiff(arr, n))
  
# This code is contributed
# by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum 
// value (arr[i]-i)- (arr[j]-j) 
// in an array.
using System;
class GFG {
      
// Returns maximum value of
// (arr[i]-i) - (arr[j]-j)
static int findMaxDiff(int []arr, int n) 
{
    if (n < 2) {
    Console.WriteLine("Invalid ");
    return 0;
    }
  
    // Use two nested loops to
    // find the result
    int res = int.MinValue;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        if (res < (arr[i] - arr[j] - i + j))
            res = (arr[i] - arr[j] - i + j);
  
    return res;
}
  
// Driver code
public static void Main() 
{
    int []arr = {9, 15, 4, 12, 13};
    int n = arr.Length;
    Console.WriteLine(findMaxDiff(arr, n));
}
}
  
// This code is contributed by anjuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find maximum value 
// (arr[i]-i) - (arr[j]-j) in an array.
  
// Returns maximum value of 
// (arr[i]-i) - (arr[j]-j)
function findMaxDiff( $arr, $n)
{
    if ($n < 2)
    {
        echo "Invalid ";
        return 0;
    }
  
    // Use two nested loops to 
    // find the result
    $res = PHP_INT_MIN;
    for($i = 0; $i < $n; $i++)
    for($j = 0; $j < $n; $j++)
        if($res < ($arr[$i] - $arr[$j] - $i + $j))
            $res = ($arr[$i] - $arr[$j] - $i + $j);
  
    return $res;
}
  
// Driver Code
$arr = array(9, 15, 4, 12, 13);
$n = count($arr);
echo findMaxDiff($arr, $n);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output:

12

 

Method 2 (Tricky : O(n))
1) Find maximum value of arr[i] – i in whole array.
2) Find minimum value of arr[i] – i in whole array.
3) Return difference of above two values.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
#include<bits/stdc++.h>
using namespace std;
  
// Returns maximum value of (arr[i]-i) - (arr[j]-j)
int findMaxDiff(int a[], int n)
{
    if (n < 2)
    {
        cout << "Invalid ";
        return 0;
    }
  
    // Find maximum of value of arr[i] - i for all
    // possible values of i. Let this value be max_val.
    // Find minimum of value of arr[i] - i for all
    // possible values of i. Let this value be min_val.
    // The difference max_val - min_v
    int min_val = INT_MAX, max_val = 
                           INT_MIN;
    for (int i=0; i<n; i++)
    {
        if ((a[i]-i) > max_val)
            max_val = a[i] - i;
        if ((a[i]-i) < min_val)
            min_val = a[i]-i;
    }
  
    return (max_val - min_val);
}
  
// Driver program
int main()
{
    int arr[] = {9, 15, 4, 12, 13};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << findMaxDiff(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
import java.io.*;
  
class GFG {
  
    // Returns maximum value of (arr[i]-i) - 
    // (arr[j]-j)
    static int findMaxDiff(int arr[], int n)    
    {
        if (n < 2)
        {
            System.out.println("Invalid ");
            return 0;
        }
  
        // Find maximum of value of arr[i] - i 
        // for all possible values of i. Let 
        // this value be max_val. Find minimum
        // of value of arr[i] - i for all
        // possible values of i. Let this value
        // be min_val. The difference max_val -
        // min_v
        int min_val = Integer.MAX_VALUE,
            max_val = Integer.MIN_VALUE;
          
        for (int i = 0; i < n; i++)
        {
            if ((arr[i]-i) > max_val)
                max_val = arr[i] - i;
                  
            if ((arr[i]-i) < min_val)
                min_val = arr[i]-i;
        }
  
        return (max_val - min_val);
    }
  
    // Driver program
    public static void main(String[] args)
    {
        int arr[] = {9, 15, 4, 12, 13};
        int n = arr.length;
        System.out.println(findMaxDiff(arr, n));
    }
}
// This code is contributed by Prerna Saini

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find maximum value
# (arr[i]-i) - (arr[j]-j) in an array.
import sys 
  
# Returns maximum value of 
# (arr[i]-i) - (arr[j]-j)
def findMaxDiff(a, n):
    if (n < 2):
        print("Invalid ")
        return 0
  
    # Find maximum of value of arr[i] - i
    # for all possible values of i. Let 
    # this value be max_val. Find minimum
    # of value of arr[i] - i for all possible 
    # values of i. Let this value be min_val.
    # The difference max_val - min_v
    min_val = sys.maxsize
    max_val = -sys.maxsize - 1
    for i in range(n):
        if ((a[i] - i) > max_val):
            max_val = a[i] - i
        if ((a[i] - i) < min_val):
            min_val = a[i] - i
  
    return (max_val - min_val)
  
# Driver Code
if __name__ == '__main__':
    arr = [9, 15, 4, 12, 13]
    n = len(arr)
    print(findMaxDiff(arr, n))
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
using System;
   
class GFG {
   
    // Returns maximum value of (arr[i]-i) - 
    // (arr[j]-j)
    static int findMaxDiff(int []arr, int n)    
    {
        if (n < 2)
        {
            Console.Write("Invalid ");
            return 0;
        }
   
        // Find maximum of value of arr[i] - i 
        // for all possible values of i. Let 
        // this value be max_val. Find minimum
        // of value of arr[i] - i for all
        // possible values of i. Let this value
        // be min_val. The difference max_val -
        // min_v
        int min_val = int.MaxValue,
            max_val = int.MinValue;
           
        for (int i = 0; i < n; i++)
        {
            if ((arr[i] - i) > max_val)
                max_val = arr[i] - i;
                   
            if ((arr[i] - i) < min_val)
                min_val = arr[i] - i;
        }
   
        return (max_val - min_val);
    }
   
    // Driver program
    public static void Main()
    {
        int []arr = {9, 15, 4, 12, 13};
        int n = arr.Length;
        Console.Write(findMaxDiff(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
// (arr[i]-i) - (arr[j]-j) in an array.
  
// Returns maximum value of 
// (arr[i]-i) - (arr[j]-j)
function findMaxDiff($a, $n)
{
    if ($n < 2)
    {
        echo "Invalid ";
        return 0;
    }
  
    // Find maximum of value of arr[i] - i 
    // for all possible values of i. Let 
    // this value be max_val. Find minimum 
    // of value of arr[i] - i for all
    // possible values of i. Let this value 
    // be min_val. The difference max_val - min_v
    $min_val = PHP_INT_MAX;
    $max_val = PHP_INT_MIN;
    for ($i = 0; $i < $n; $i++)
    {
        if (($a[$i] - $i) > $max_val)
            $max_val = $a[$i] - $i;
        if (($a[$i] - $i) < $min_val)
            $min_val = $a[$i] - $i;
    }
  
    return ($max_val - $min_val);
}
  
// Driver Code
$arr = array(9, 15, 4, 12, 13);
$n = sizeof($arr);
echo findMaxDiff($arr, $n);
  
// This code is contributed by Sachin.
?>

chevron_right



Output:

12

This article is contributed by Shivam Agrawal. 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 :


Be the First to upvote.


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