Maximize value of (a[i]+i)*(a[j]+j) in an array
Last Updated :
20 Jul, 2022
Given an array with input size n, find the maximum value of (a[i] + i) * (a[j] + j) where i is not equal to j.
Note that i and j vary from 0 to n-1 .
Examples:
Input : a[] = [4,5,3,1,10]
Output : 84
Explanation:
We get the maximum value for i = 4 and j = 1
(10 + 4) * (5 + 1) = 84
Input : a[] = [10,0,0,0,-1]
Output : 30
Explanation:
We get the maximum value for i = 0 and j = 3
(10 + 0) * (0 + 3) = 30
Naive approach: The simplest way is to run two loops to consider all possible pairs and keep track of maximum value of expression (a[i]+i)*(a[j]+j). Below is Python implementation of this idea. Time complexity will be O(n*n) where n is the input size.
Implementation:
C++
// C++ program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers. maxval()
// returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
#include<bits/stdc++.h>
using namespace std;
int maxval(int a[], int n) {
// at-least there must be two elements
// in array
if (n < 2) {
return -99999;
}
// calculate maximum value
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int x = (a[i] + i) * (a[j] + j);
if (max < x) {
max = x;
}
}
}
return max;
}
// test the function
int main()
{
int arr[] = {4, 5, 3, 1, 10};
int len = sizeof(arr)/sizeof(arr[0]);
cout<<(maxval(arr, len));
}
// This code is contributed by
// Shashank_Sharma
Java
// Java program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers. maxval()
// returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
public class GFG {
// Python
static int maxval(int a[], int n) {
// at-least there must be two elements
// in array
if (n < 2) {
return -99999;
}
// calculate maximum value
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int x = (a[i] + i) * (a[j] + j);
if (max < x) {
max = x;
}
}
}
return max;
}
// test the function
public static void main(String args[]) {
int arr[] = {4, 5, 3, 1, 10};
int len = arr.length;
System.out.println(maxval(arr, len));
}
}
/*This code is contributed by 29AjayKumar*/
Python3
# Python program to find maximum value (a[i]+i)*
# (a[j]+j) in an array of integers. maxval()
# returns maximum value of (a[i]+i)*(a[j]+j)
# where i is not equal to j
def maxval(a,n):
# at-least there must be two elements
# in array
if (n < 2):
return -99999
# calculate maximum value
max = 0
for i in range(n):
for j in range(i+1,n):
x = (a[i]+i)*(a[j]+j)
if max < x:
max = x
return max
# test the function
print(maxval([4,5,3,1,10],5))
C#
// C# program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers. maxval()
// returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
using System;
public class GFG {
// Python
static int maxval(int []a, int n) {
// at-least there must be two elements
// in array
if (n < 2) {
return -99999;
}
// calculate maximum value
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int x = (a[i] + i) * (a[j] + j);
if (max < x) {
max = x;
}
}
}
return max;
}
// test the function
public static void Main() {
int []arr = {4, 5, 3, 1, 10};
int len = arr.Length;
Console.Write(maxval(arr, len));
}
}
/*This code is contributed by 29AjayKumar*/
PHP
<?php
// PHP program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers. maxval()
// returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to on
function maxval($a, $n)
{
// at-least there must be two
// elements in array
if ($n < 2)
{
return -99999;
}
// calculate maximum value
$max = 0;
for ($i = 0; $i < $n; $i++)
{
for ($j = $i + 1; $j < $n; $j++)
{
$x = ($a[$i] + $i) * ($a[$j] + $j);
if ($max < $x)
{
$max = $x;
}
}
}
return $max;
}
// Driver Code
$arr = array(4, 5, 3, 1, 10);
$len = count($arr);
echo (maxval($arr, $len));
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers. maxval()
// returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
function maxval(a, n) {
// at-least there must be two elements
// in array
if (n < 2) {
return -99999;
}
// calculate maximum value
let max = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let x = (a[i] + i) * (a[j] + j);
if (max < x) {
max = x;
}
}
}
return max;
}
let arr = [4, 5, 3, 1, 10];
let len = arr.length;
document.write(maxval(arr, len));
</script>
Efficient approach:
An efficient method is to find maximum value of a[i] + i along with the second maximum value of a[i] + i in the array. Return the product of the two values.
Finding maximum and second maximum can be done in a single traversal of the array.
So,Time complexity will be O(n).
Below is the implementation of this idea.
C++
// C++ program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers
// maxval() returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
#include<bits/stdc++.h>
using namespace std;
#define MAX 5
int maxval(int a[MAX], int n)
{
// there must be at-least two
// elements in the array
if (n < 2)
{
cout << "Invalid Input";
return -9999;
}
// max1 will store the maximum value of
// (a[i]+i)
// max2 will store the second maximum value
// of (a[i]+i)
int max1 = 0, max2 = 0;
for (int i = 0; i < n; i++)
{
int x = a[i] + i;
// If current element x is greater than
// first then update first and second
if (x > max1)
{
max2 = max1;
max1 = x;
}
// if x is in between max1 and
// max2 then update max2
else if (x > max2 & x != max1)
{
max2 = x;
}
}
return (max1 * max2);
}
// Driver Code
int main()
{
int arr[] = {4, 5, 3, 1, 10};
int len = sizeof(arr)/arr[0];
cout << maxval(arr, len);
}
// This code is contributed
// by Akanksha Rai
C
// C program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers
// maxval() returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
#include<stdio.h>
#include<string.h>
#define MAX 5
int maxval(int a[MAX], int n) {
// there must be at-least two elements in
// the array
if (n < 2) {
printf("Invalid Input");
return -9999;
}
// max1 will store the maximum value of
// (a[i]+i)
// max2 will store the second maximum value
// of (a[i]+i)
int max1 = 0, max2 = 0;
for (int i = 0; i < n; i++) {
int x = a[i] + i;
// If current element x is greater than
// first then update first and second
if (x > max1) {
max2 = max1;
max1 = x;
}// if x is in between max1 and
// max2 then update max2
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
// test the function
}
int main() {
int arr[] = {4, 5, 3, 1, 10};
int len = sizeof(arr)/arr[0];
printf("%d",maxval(arr, len));
}
// This code is contributed by 29AjayKumar
Java
// Java program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers
// maxval() returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
class GFG {
static int maxval(int[] a, int n) {
// there must be at-least two elements in
// the array
if (n < 2) {
System.out.print("Invalid Input");
return -9999;
}
// max1 will store the maximum value of
// (a[i]+i)
// max2 will store the second maximum value
// of (a[i]+i)
int max1 = 0, max2 = 0;
for (int i = 0; i < n; i++) {
int x = a[i] + i;
// If current element x is greater than
// first then update first and second
if (x > max1) {
max2 = max1;
max1 = x;
} // if x is in between max1 and
// max2 then update max2
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
// test the function
}
public static void main(String[] args) {
int arr[] = {4, 5, 3, 1, 10};
int len = arr.length;
System.out.println(maxval(arr, len));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python program to find maximum value (a[i]+i)*
# (a[j]+j) in an array of integers
# maxval() returns maximum value of (a[i]+i)*(a[j]+j)
# where i is not equal to j
def maxval(a,n):
# there must be at-least two elements in
# the array
if (n < 2):
print("Invalid Input")
return -9999
# max1 will store the maximum value of
# (a[i]+i)
# max2 will store the second maximum value
# of (a[i]+i)
(max1, max2) = (0, 0)
for i in range(n):
x = a[i] + i
# If current element x is greater than
# first then update first and second
if (x > max1):
max2 = max1
max1 = x
# if x is in between max1 and
# max2 then update max2
elif (x > max2 and x != max1):
max2 = x
return(max1*max2)
# test the function
print(maxval([4,5,3,1,10],5))
C#
// C# program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers
// maxval() returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
using System;
public class GFG {
static int maxval(int[] a, int n) {
// there must be at-least two elements in
// the array
if (n < 2) {
Console.WriteLine("Invalid Input");
return -9999;
}
// max1 will store the maximum value of
// (a[i]+i)
// max2 will store the second maximum value
// of (a[i]+i)
int max1 = 0, max2 = 0;
for (int i = 0; i < n; i++) {
int x = a[i] + i;
// If current element x is greater than
// first then update first and second
if (x > max1) {
max2 = max1;
max1 = x;
} // if x is in between max1 and
// max2 then update max2
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
// test the function
}
public static void Main() {
int []arr = {4, 5, 3, 1, 10};
int len = arr.Length;
Console.WriteLine(maxval(arr, len));
}
}
// This code is contributed by PrinciRaj1992
PHP
<?php
// PHP program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers
// maxval() returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
// $MAX = 5;
function maxval($a, $n)
{
// there must be at-least two elements in
// the array
if ($n < 2)
{
echo ("Invalid Input");
return -9999;
}
// max1 will store the maximum value of
// (a[i]+i)
// max2 will store the second maximum value
// of (a[i]+i)
$max1 = 0;
$max2 = 0;
for ($i = 0; $i < $n; $i++)
{
$x = $a[$i] + $i;
// If current element x is greater than
// first then update first and second
if ($x > $max1)
{
$max2 = $max1;
$max1 = $x;
}
// if x is in between max1 and
// max2 then update max2
else if (($x > $max2) & ($x != $max1))
{
$max2 = $x;
}
}
return ($max1 * $max2);
}
// Driver Code
$arr = array(4, 5, 3, 1, 10);
$len = count($arr);
echo maxval($arr, $len);
// This code is contributed by ajit.
?>
JavaScript
<script>
// Javascript program to find maximum value (a[i]+i)*
// (a[j]+j) in an array of integers
// maxval() returns maximum value of (a[i]+i)*(a[j]+j)
// where i is not equal to j
function maxval(a, n)
{
// there must be at-least two elements in
// the array
if (n < 2) {
document.write("Invalid Input");
return -9999;
}
// max1 will store the maximum value of
// (a[i]+i)
// max2 will store the second maximum value
// of (a[i]+i)
let max1 = 0, max2 = 0;
for (let i = 0; i < n; i++) {
let x = a[i] + i;
// If current element x is greater than
// first then update first and second
if (x > max1)
{
max2 = max1;
max1 = x;
}
// if x is in between max1 and
// max2 then update max2
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
// test the function
}
let arr = [4, 5, 3, 1, 10];
let len = arr.length;
document.write(maxval(arr, len));
</script>
Similar Reads
Maximum value of |arr[i] - arr[j]| + |i - j| Given a array of N positive integers. The task is to find the 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 : 4Explanation:Choose i = 0 and j = 2. This will result in |1-3|+|
15+ 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 value of arr[i] + arr[j] + i â j for any pair of an array 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: 13Explanation:The pair of the array having the maximum value of (arr[i] + arr[j] + i ? j)
9 min read
Maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array Given an array A[] of length N and an integer K, the task is to maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array, where (1 ⤠i < j ⤠N) and | denotes Bitwise OR operator. Examples: Input: A[] = {5, 20, 1, 0, 8, 11}, K = 10Output: 2Explanation: The maximum
6 min read
Maximize the Absolute Value of Expression |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j| Given two integer arrays arr1[] and arr2[] of equal length, the task is to find the maximum value of the expression: |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|, where the maximum is taken over all valid indices i and j satisfying 0 <= i, j < arr1.length. Return the maximum value of th
7 min read
Find triplet (i, j, k) to maximize (P * arr[i] + Q * arr[j] + R * arr[k]) Given an array arr[] of size N and three numbers P, Q, and R. The task is to find three indices named (i, j, k) in the given array such that i < j < k, and the value of equation (P * arr[i] + Q * arr[j] + R * arr[k]) should be maximum. Examples: Input: arr[] = {1, 2, 3, 4, 5}, P = 1, Q = 2, R
15+ min read