最大化数组中 (a[i]+i)*(a[j]+j) 的值
给定一个输入大小为 n 的数组,求 (a[i] + i) * (a[j] + j) 的最大值,其中 i不等于j。
请注意, i 和 j 从0到n-1变化。
例子:
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
天真的方法:最简单的方法是运行两个循环来考虑所有可能的对并跟踪表达式 (a[i]+i)*(a[j]+j) 的最大值。下面是这个想法的Python实现。时间复杂度为 O(n*n) ,其中 n 是输入大小。
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
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
Javascript
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
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
#include
#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
$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
输出:
84
有效的方法:一种有效的方法是在数组中找到 a[i] + i 的最大值以及 a[i] + i 的第二个最大值。返回两个值的乘积。
查找最大值和第二个最大值可以在数组的单次遍历中完成。
因此,时间复杂度将为 O(n) 。
下面是这个想法的实现。
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
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
#include
#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
$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
输出:
84