The Wayback Machine - https://web.archive.org/web/20210414112749/https://www.geeksforgeeks.org/maximum-value-of-arri-arrj-i-j-for-any-pair-of-an-array/
Skip to content
Related Articles

Related Articles

Maximum value of arr[i] + arr[j] + i – j for any pair of an array
  • Last Updated : 14 Apr, 2021
GeeksforGeeks - Summer Carnival Banner

Given an array arr[] consisting of N integers, the task is to find the maximum value of (arr[i] + arr[j] + i − j) for any possible pair (i, j) of the given array.

Examples:

Input: arr[] = {1, 9, 3, 6, 5}
Output: 13
Explanation:
The pair of the array having the maximum value of (arr[i] + arr[j] + i − j) is (1, 3). The value is (arr[1] + arr[3] + 1 – 3) = (9 + 6 + 1 – 3) = 13. 

Input: arr[] = {6, 2, 5, 6}
Output: 10

Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs (i, j) of the given array and find the maximum value of the expression among all possible pairs.



Below is the implementation of the above approach:

C++




// C++ program to for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
void maximumValue(int arr[], int n)
{
    // Stores the required result
    int ans = 0;
  
    // Traverse over all the pairs (i, j)
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
  
            // Calculate the value of the
            // expression and update the
            // overall maximum value
            ans = max(ans, arr[i] + arr[j]
                               + i - j);
        }
    }
  
    // Print the result
    cout << ans;
}
  
// Driven Code
int main()
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    maximumValue(arr, N);
  
    return 0;
}
Output:
13

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by breaking the expression (arr[i] + arr[j] + i – j) in two parts: (arr[i] + i) and (arr[j] – j) and then maximize the sum of the maximum value of (arr[i] + i) with all possible value of (arr[i] – i). Follow the steps below to solve the problem:

  • Initialize two variables, res with 0 to store the required sum and maxValue with arr[0] to keep track of the maximum value of (arr[i] + i).
  • Traverse the given array arr[] over the range [1, N – 1] using the variable i and perform the following steps:
    • Store the value of the expression in X as (maxValue + arr[i] – i).
    • If the value of X is greater than res, then update the value of res to X.
    • If the value of arr[i] + i is greater than maxValue, then update maxValue to (arr[i] + i). 
  • After completing the above steps, print the value of res as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the maximum value
// of (arr[i] + arr[j] + i - j)
// possible for a pair in the array
void maximumValue(int arr[], int n)
{
    // Stores the maximum value
    // of(arr[i] + i)
    int maxvalue = arr[0];
  
    // Stores the required result
    int result = 0;
  
    // Traverse the array arr[]
    for (int i = 1; i < n; i++) {
  
        // Calculate for current pair
        // and update maximum value
        result = max(result,
                     maxvalue + arr[i] - i);
  
        // Update maxValue if (arr[i] + I)
        // is greater than maxValue
        maxvalue = max(maxvalue, arr[i] + i);
    }
  
    // Print the result
    cout << result;
}
  
// Driven Code
int main()
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    maximumValue(arr, N);
  
    return 0;
}
Output:
13

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :