给定一个大小为N的数组arr[] ,任务是在用它们的和重复替换一对相邻数组元素后,找到剩余对的最大乘积。
注意:将数组减少到大小为 2。
例子:
Input: arr[] = {2, 3, 5, 6, 7}
Output: 130
Explanation:
Replacing arr[1] and arr[2] with their sum (i.e. 3 + 5 = 8) modifies arr[] to {2, 8, 6, 7}
Replacing arr[2] and arr[3] with their sum (i.e. 6 + 7 = 13) modifies arr[] to {2, 8, 13}
Replacing arr[0] and arr[1] with their sum (2 + 8 = 10) modifies arr[] to {10, 13}
Maximum Product of the remaining pair = 10 * 13 = 130
Input: arr[] = {5, 6}
Output: 30
方法:给定的问题可以通过观察来解决。可以观察到,对于索引i , X必须等于前i 个元素的总和,即 arr[1] + arr[2] + arr[3] + … + arr[i] 并且Y必须相等到其余元素的总和,即 arr[i + 1] + arr[i + 2] +…+ arr[N]。现在,这个问题可以通过使用前缀 sum 并找到它与每个索引处其余元素的总和的乘积来解决。请按照以下步骤解决问题:
- 将ans初始化为 INT_MIN 以存储所需的答案,将prefixSum 初始化为0以存储数组的前缀和。
- 将数组元素的总和存储在一个变量中,比如S 。
- 使用变量i在索引范围[0, N – 2] 上遍历数组并执行以下操作:
- 将arr[i]的值添加到prefixSum 。
- 将prefixSum的值存储在变量X 中,并将(sum – prefixSum)存储在变量Y 中。
- 如果(X * Y)的值大于ans ,则将ans更新为(X * Y) 。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum product
// possible after repeatedly replacing
// pairs of adjacent array elements
// with their sum
void maxProduct(int arr[], int N)
{
// Store the maximum product
int max_product = INT_MIN;
// Store the prefix sum
int prefix_sum = 0;
// Store the total sum of array
int sum = 0;
// Traverse the array to find
// the total sum
for (int i = 0; i < N; i++) {
sum += arr[i];
}
// Iterate in the range [0, N-2]
for (int i = 0; i < N - 1; i++) {
// Add arr[i] to prefix_sum
prefix_sum += arr[i];
// Store the value of prefix_sum
int X = prefix_sum;
// Store the value of
// (total sum - prefix sum)
int Y = sum - prefix_sum;
// Update the maximum product
max_product = max(max_product,
X * Y);
}
// Print the answer
cout << max_product;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 5, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
maxProduct(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the maximum product
// possible after repeatedly replacing
// pairs of adjacent array elements
// with their sum
static void maxProduct(int[] arr, int N)
{
// Store the maximum product
int max_product = Integer.MIN_VALUE;
// Store the prefix sum
int prefix_sum = 0;
// Store the total sum of array
int sum = 0;
// Traverse the array to find
// the total sum
for (int i = 0; i < N; i++)
{
sum += arr[i];
}
// Iterate in the range [0, N-2]
for (int i = 0; i < N - 1; i++)
{
// Add arr[i] to prefix_sum
prefix_sum += arr[i];
// Store the value of prefix_sum
int X = prefix_sum;
// Store the value of
// (total sum - prefix sum)
int Y = sum - prefix_sum;
// Update the maximum product
max_product = Math.max(max_product, X * Y);
}
// Print the answer
System.out.print(max_product);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 3, 5, 6, 7 };
int N = arr.length;
maxProduct(arr, N);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python program for the above approach
import sys
# Function to find the maximum product
# possible after repeatedly replacing
# pairs of adjacent array elements
# with their sum
def maxProduct(arr, N):
# Store the maximum product
max_product = -sys.maxsize;
# Store the prefix sum
prefix_sum = 0;
# Store the total sum of array
sum = 0;
# Traverse the array to find
# the total sum
for i in range(N):
sum += arr[i];
# Iterate in the range [0, N-2]
for i in range(N - 1):
# Add arr[i] to prefix_sum
prefix_sum += arr[i];
# Store the value of prefix_sum
X = prefix_sum;
# Store the value of
# (total sum - prefix sum)
Y = sum - prefix_sum;
# Update the maximum product
max_product = max(max_product, X * Y);
# Prthe answer
print(max_product);
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 5, 6, 7];
N = len(arr);
maxProduct(arr, N);
# This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the maximum product
// possible after repeatedly replacing
// pairs of adjacent array elements
// with their sum
static void maxProduct(int[] arr, int N)
{
// Store the maximum product
int max_product = Int32.MinValue;
// Store the prefix sum
int prefix_sum = 0;
// Store the total sum of array
int sum = 0;
// Traverse the array to find
// the total sum
for (int i = 0; i < N; i++)
{
sum += arr[i];
}
// Iterate in the range [0, N-2]
for (int i = 0; i < N - 1; i++)
{
// Add arr[i] to prefix_sum
prefix_sum += arr[i];
// Store the value of prefix_sum
int X = prefix_sum;
// Store the value of
// (total sum - prefix sum)
int Y = sum - prefix_sum;
// Update the maximum product
max_product = Math.Max(max_product, X * Y);
}
// Print the answer
Console.WriteLine(max_product);
}
// Driver code
static void Main()
{
int[] arr = { 2, 3, 5, 6, 7 };
int N = arr.Length;
maxProduct(arr, N);
}
}
// This code is contributed by divyeshrabadiya07.
Javascript
130
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live