Maximum value of arr[i] + arr[j] + i – j for any pair of an array
Last Updated :
25 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void maximumValue( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
ans = max(ans, arr[i] + arr[j]
+ i - j);
}
}
cout << ans;
}
int main()
{
int arr[] = { 1, 9, 3, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
maximumValue(arr, N);
return 0;
}
|
Java
class GFG{
static void maximumValue( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
ans = Math.max(ans,
arr[i] + arr[j] + i - j);
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 9 , 3 , 6 , 5 };
int N = arr.length;
maximumValue(arr, N);
}
}
|
Python3
def maximumValue(arr, n):
ans = 0
for i in range (n):
for j in range (i + 1 , n):
ans = max (ans, arr[i] + arr[j] + i - j)
print (ans)
arr = [ 1 , 9 , 3 , 6 , 5 ]
N = len (arr)
maximumValue(arr, N)
|
C#
using System;
class GFG{
static void maximumValue( int [] arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
ans = Math.Max(ans, arr[i] + arr[j] + i - j);
}
}
Console.Write(ans);
}
static void Main()
{
int [] arr = { 1, 9, 3, 6, 5 };
int N = arr.Length;
maximumValue(arr, N);
}
}
|
Javascript
<script>
var arr = [ 1, 9, 3, 6, 5 ];
function maximumValue(arr, n)
{
var ans = 0;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
ans = Math.max(ans, arr[i] + arr[j] + i - j);
}
}
document.write(ans);
}
n = arr.length
maximumValue(arr, n);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
void maximumValue( int arr[], int n)
{
int maxvalue = arr[0];
int result = 0;
for ( int i = 1; i < n; i++) {
result = max(result,
maxvalue + arr[i] - i);
maxvalue = max(maxvalue, arr[i] + i);
}
cout << result;
}
int main()
{
int arr[] = { 1, 9, 3, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
maximumValue(arr, N);
return 0;
}
|
Java
class GFG{
static void maximumValue( int arr[], int n)
{
int maxvalue = arr[ 0 ];
int result = 0 ;
for ( int i = 1 ; i < n; i++)
{
result = Math.max(result,
maxvalue + arr[i] - i);
maxvalue = Math.max(maxvalue, arr[i] + i);
}
System.out.println(result);
}
public static void main(String[] args)
{
int arr[] = { 1 , 9 , 3 , 6 , 5 };
int N = arr.length;
maximumValue(arr, N);
}
}
|
Python3
def maximumValue(arr, n):
maxvalue = arr[ 0 ]
result = 0
for i in range ( 1 , n):
result = max (result, maxvalue + arr[i] - i)
maxvalue = max (maxvalue, arr[i] + i)
print (result)
arr = [ 1 , 9 , 3 , 6 , 5 ]
N = len (arr)
maximumValue(arr, N)
|
C#
using System;
class GFG{
static void maximumValue( int [] arr, int n)
{
int maxvalue = arr[0];
int result = 0;
for ( int i = 1; i < n; i++)
{
result = Math.Max(result,
maxvalue + arr[i] - i);
maxvalue = Math.Max(maxvalue, arr[i] + i);
}
Console.Write(result);
}
static void Main()
{
int [] arr = { 1, 9, 3, 6, 5 };
int N = arr.Length;
maximumValue(arr, N);
}
}
|
Javascript
<script>
function maximumValue(arr , n)
{
var maxvalue = arr[0];
var result = 0;
for (i = 1; i < n; i++) {
result = Math.max(result, maxvalue + arr[i] - i);
maxvalue = Math.max(maxvalue, arr[i] + i);
}
document.write(result);
}
var arr = [ 1, 9, 3, 6, 5 ];
var N = arr.length;
maximumValue(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...