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++
#include<bits/stdc++.h>
using namespace std;
int maxval( int a[], int n) {
if (n < 2) {
return -99999;
}
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;
}
int main()
{
int arr[] = {4, 5, 3, 1, 10};
int len = sizeof (arr)/ sizeof (arr[0]);
cout<<(maxval(arr, len));
}
|
Java
public class GFG {
static int maxval( int a[], int n) {
if (n < 2 ) {
return - 99999 ;
}
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;
}
public static void main(String args[]) {
int arr[] = { 4 , 5 , 3 , 1 , 10 };
int len = arr.length;
System.out.println(maxval(arr, len));
}
}
|
Python3
def maxval(a,n):
if (n < 2 ):
return - 99999
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
print (maxval([ 4 , 5 , 3 , 1 , 10 ], 5 ))
|
C#
using System;
public class GFG {
static int maxval( int []a, int n) {
if (n < 2) {
return -99999;
}
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;
}
public static void Main() {
int []arr = {4, 5, 3, 1, 10};
int len = arr.Length;
Console.Write(maxval(arr, len));
}
}
|
PHP
<?php
function maxval( $a , $n )
{
if ( $n < 2)
{
return -99999;
}
$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 ;
}
$arr = array (4, 5, 3, 1, 10);
$len = count ( $arr );
echo (maxval( $arr , $len ));
?>
|
Javascript
<script>
function maxval(a, n) {
if (n < 2) {
return -99999;
}
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++
#include<bits/stdc++.h>
using namespace std;
#define MAX 5
int maxval( int a[MAX], int n)
{
if (n < 2)
{
cout << "Invalid Input" ;
return -9999;
}
int max1 = 0, max2 = 0;
for ( int i = 0; i < n; i++)
{
int x = a[i] + i;
if (x > max1)
{
max2 = max1;
max1 = x;
}
else if (x > max2 & x != max1)
{
max2 = x;
}
}
return (max1 * max2);
}
int main()
{
int arr[] = {4, 5, 3, 1, 10};
int len = sizeof (arr)/arr[0];
cout << maxval(arr, len);
}
|
C
#include<stdio.h>
#include<string.h>
#define MAX 5
int maxval( int a[MAX], int n) {
if (n < 2) {
printf ( "Invalid Input" );
return -9999;
}
int max1 = 0, max2 = 0;
for ( int i = 0; i < n; i++) {
int x = a[i] + i;
if (x > max1) {
max2 = max1;
max1 = x;
}
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
}
int main() {
int arr[] = {4, 5, 3, 1, 10};
int len = sizeof (arr)/arr[0];
printf ( "%d" ,maxval(arr, len));
}
|
Java
class GFG {
static int maxval( int [] a, int n) {
if (n < 2 ) {
System.out.print( "Invalid Input" );
return - 9999 ;
}
int max1 = 0 , max2 = 0 ;
for ( int i = 0 ; i < n; i++) {
int x = a[i] + i;
if (x > max1) {
max2 = max1;
max1 = x;
}
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
}
public static void main(String[] args) {
int arr[] = { 4 , 5 , 3 , 1 , 10 };
int len = arr.length;
System.out.println(maxval(arr, len));
}
}
|
Python3
def maxval(a,n):
if (n < 2 ):
print ( "Invalid Input" )
return - 9999
(max1, max2) = ( 0 , 0 )
for i in range (n):
x = a[i] + i
if (x > max1):
max2 = max1
max1 = x
elif (x > max2 and x ! = max1):
max2 = x
return (max1 * max2)
print (maxval([ 4 , 5 , 3 , 1 , 10 ], 5 ))
|
C#
using System;
public class GFG {
static int maxval( int [] a, int n) {
if (n < 2) {
Console.WriteLine( "Invalid Input" );
return -9999;
}
int max1 = 0, max2 = 0;
for ( int i = 0; i < n; i++) {
int x = a[i] + i;
if (x > max1) {
max2 = max1;
max1 = x;
}
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
}
public static void Main() {
int []arr = {4, 5, 3, 1, 10};
int len = arr.Length;
Console.WriteLine(maxval(arr, len));
}
}
|
PHP
<?php
function maxval( $a , $n )
{
if ( $n < 2)
{
echo ( "Invalid Input" );
return -9999;
}
$max1 = 0;
$max2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$x = $a [ $i ] + $i ;
if ( $x > $max1 )
{
$max2 = $max1 ;
$max1 = $x ;
}
else if (( $x > $max2 ) & ( $x != $max1 ))
{
$max2 = $x ;
}
}
return ( $max1 * $max2 );
}
$arr = array (4, 5, 3, 1, 10);
$len = count ( $arr );
echo maxval( $arr , $len );
?>
|
Javascript
<script>
function maxval(a, n)
{
if (n < 2) {
document.write( "Invalid Input" );
return -9999;
}
let max1 = 0, max2 = 0;
for (let i = 0; i < n; i++) {
let x = a[i] + i;
if (x > max1)
{
max2 = max1;
max1 = x;
}
else if (x > max2 & x != max1) {
max2 = x;
}
}
return (max1 * max2);
}
let arr = [4, 5, 3, 1, 10];
let len = arr.length;
document.write(maxval(arr, len));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...