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++
// 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;
}
Java
// Java program for the above approach
class GFG{
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
static 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 = Math.max(ans,
arr[i] + arr[j] + i - j);
}
}
// Print the result
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 9, 3, 6, 5 };
int N = arr.length;
maximumValue(arr, N);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find the maximum value of
# arr[i] + arr[j] + i - j over all pairs
def maximumValue(arr, n):
# Stores the required result
ans = 0
# Traverse over all the pairs (i, j)
for i in range(n):
for j in range(i + 1, n):
# Calculate the value of the
# expression and update the
# overall maximum value
ans = max(ans, arr[i] + arr[j] + i - j)
print(ans)
# Driver Code
arr = [ 1, 9, 3, 6, 5 ]
N = len(arr)
maximumValue(arr, N)
# This code is contributed by abhinavjain194
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
static 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 = Math.Max(ans, arr[i] + arr[j] + i - j);
}
}
// Print the result
Console.Write(ans);
}
// Driver code
static void Main()
{
int[] arr = { 1, 9, 3, 6, 5 };
int N = arr.Length;
maximumValue(arr, N);
}
}
// This code is contributed by abhinavjain194
JavaScript
<script>
// Javascript program to for the above approach
var arr = [ 1, 9, 3, 6, 5 ];
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
function maximumValue(arr, n)
{
// Stores the required result
var ans = 0;
// Traversing over all the pairs (i, j)
for(i = 0; i < n; i++)
{
for(j = i + 1; j < n; j++)
{
// Calculate the value of the
// expression and update the
// overall maximum value
ans = Math.max(ans, arr[i] + arr[j] + i - j);
}
}
// Print the result
document.write(ans);
}
// Driver code
n = arr.length
maximumValue(arr, n);
// This code is contributed by SoumikMondal
</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++
// 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;
}
Java
// Java program for the above approach
class GFG{
// Function to find the maximum value
// of (arr[i] + arr[j] + i - j)
// possible for a pair in the array
static 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 = Math.max(result,
maxvalue + arr[i] - i);
// Update maxValue if (arr[i] + I)
// is greater than maxValue
maxvalue = Math.max(maxvalue, arr[i] + i);
}
// Print the result
System.out.println(result);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 9, 3, 6, 5 };
int N = arr.length;
maximumValue(arr, N);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find the maximum value
# of (arr[i] + arr[j] + i - j)
# possible for a pair in the array
def maximumValue(arr, n):
# Stores the maximum value
# of(arr[i] + i)
maxvalue = arr[0]
# Stores the required result
result = 0
# Traverse the array arr[]
for i in range(1, n):
# 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(result)
# Driver code
arr = [ 1, 9, 3, 6, 5 ]
N = len(arr)
maximumValue(arr, N)
# This code is contributed by abhinavjain194
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
static 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 = Math.Max(result,
maxvalue + arr[i] - i);
// Update maxValue if (arr[i] + I)
// is greater than maxValue
maxvalue = Math.Max(maxvalue, arr[i] + i);
}
// Print the result
Console.Write(result);
}
// Driver code
static void Main()
{
int[] arr = { 1, 9, 3, 6, 5 };
int N = arr.Length;
maximumValue(arr, N);
}
}
// This code is contributed by abhinavjain194
JavaScript
<script>
// Javascript program for the above approach
// Function to find the maximum value
// of (arr[i] + arr[j] + i - j)
// possible for a pair in the array
function maximumValue(arr , n)
{
// Stores the maximum value
// of(arr[i] + i)
var maxvalue = arr[0];
// Stores the required result
var result = 0;
// Traverse the array arr
for (i = 1; i < n; i++) {
// Calculate for current pair
// and update maximum value
result = Math.max(result, maxvalue + arr[i] - i);
// Update maxValue if (arr[i] + I)
// is greater than maxValue
maxvalue = Math.max(maxvalue, arr[i] + i);
}
// Print the result
document.write(result);
}
// Driver Code
var arr = [ 1, 9, 3, 6, 5 ];
var N = arr.length;
maximumValue(arr, N);
// This code contributed by Rajput-Ji
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximum value of (arr[i] * arr[j]) + (arr[j] - arr[i])) possible for any pair in an array Given an array arr[] consisting of N integers, the task is to find the maximum possible value of the expression (arr[i] * arr[j]) + (arr[j] - arr[i])) for any pair (i, j), such that i ? j and 0 ? (i, j) < N. Examples: Input: arr[] = {-2, -8, 0, 1, 2, 3, 4}Output: 22Explanation:For the pair (-8, -
6 min read
Maximum value of arr[i] % arr[j] for a given array Given an array arr[], the task is to find the maximum value of arr[i] % arr[j] for all possible pairs.Examples: Input: arr[] = { 2, 3 } Output: 2 2 % 3 = 2 3 % 2 = 1 Input: arr[] = { 2, 2, 2, 2 } Output: 0 Naive Approach: Run two nested loops and calculate the value of arr[i] % arr[j] for every pair
13 min read
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 In
10 min read
Find Maximum value of abs(i - j) * min(arr[i], arr[j]) in an array arr[] Given an array of n distinct elements. Find the maximum of product of Minimum of two numbers in the array and absolute difference of their positions, i.e., find maximum value of abs(i - j) * min(arr[i], arr[j]) where i and j vary from 0 to n-1. Examples : Input : arr[] = {3, 2, 1, 4} Output: 9 // ar
6 min read
Maximum AND value of a pair in an array Given an array of N positive elements, the task is to find the maximum AND value generated by any pair of elements from the array. Examples: Input: arr1[] = {16, 12, 8, 4}Output: 8Explanation: 8 AND12 will give us the maximum value 8 Input: arr1[] = {4, 8, 16, 2}Output: 0 Recommended PracticeMaximum
12 min read