通过选择对并将一个部分除以另一个部分乘以 K 来最大化数组总和
给定一个大小为N和整数K的数组arr[] ,任务是最大化 通过执行以下操作任意次数(可能为零)得到数组的总和:
- 选择两个索引i和j其中arr[i]必须是K的倍数。
- 使arr[i] = arr[i]/K和arr[j] = K*arr[j]
- 执行上述操作后,数组元素的总和必须是最大的。
例子:
Input: arr[] = {6, 6, 3}, N = 3, K = 3
Output: 57
Explanation: The optimal sequence for which sum of the array elements would be maximized is:
- Take i = 0 and j = 1, arr[0] = arr[0]/3 = 2, arr[1] = arr[1]*3 = 18, now the new array becomes, [2, 18, 3]
- Take i = 2 and j = 1, arr[2] = arr[2]/3 = 1, arr[1] = arr[1]*3 = 54, the array becomes [2, 54, 1]
Sum = 2 + 54 + 1 = 57
Input: arr[] = {1, 2, 3, 4, 5}, N = 5, K = 2
Output: 46
Explanation: The operation performed is:
Take i = 1 and j = 4, arr[1] = arr[1]/2 = 1, arr[4] = arr[4]*2 = 10. Now arr[] = {1, 1, 3, 4, 10}.
Take i = 3 and j = 4, arr[3] = arr[3]/2 = 2, arr[4] = arr[4]*2 = 20. Now arr[] = {1, 1, 3, 2, 20}.
Take i = 3 and j = 4, arr[3] = arr[3]/2 = 1, arr[4] = arr[4]*2 = 40. Now arr[] = {1, 1, 3, 1, 40}
Sum = 1 + 1+ 3 + 1 + 40 = 46.
方法:贪心方法是将所有K的倍数的数组元素除以 K 并计算执行的除法操作(例如X )的总数。然后对数组进行排序并将数组的最大元素X乘以K即arr[N-1] = arr[N-1]*K X 。请按照以下步骤解决此问题:
- 创建一个变量total_division = 0 ,以存储执行的除法总数。
- 使用变量index遍历范围[0, N)并执行以下任务:
- 如果arr[index] %K = 0 ,则找出arr[index]可以除以K多少次,然后将total_division增加多少次。
- 对数组arr[]进行排序。
- 运行 while 循环直到total_division > 0并使arr[N-1] = arr[N-1] * K并递减total_division。
- 创建一个变量maximum_sum = 0。
- 使用变量index遍历范围[0, N)并执行以下任务:
- 更新maximum_sum += arr[index] 。
- 执行上述步骤后,打印maximum_sum的值作为答案。
以下是上述方法的实现。
C++
// C++ code for the above approach
#include
using namespace std;
// Utility function
int MaximumSumOperations(int arr[], int N,
int K)
{
if (N == 1) {
return arr[0];
}
// Variable to count total operations
int total_division = 0;
// Perform the division operation on
// the elements multiple of K
for (int index = 0; index < N;
index++) {
if (arr[index] % K == 0) {
while (arr[index] > 1
&& arr[index] % K == 0) {
arr[index] = arr[index] / K;
total_division++;
}
}
}
sort(arr, arr + N);
// Update maximum element and
// decrement total_division
while (total_division > 0) {
arr[N - 1] = K * arr[N - 1];
total_division--;
}
int maximum_sum = 0;
for (int index = 0; index < N;
index++) {
maximum_sum += arr[index];
}
// Return maximum_sum
return maximum_sum;
}
// Driver Code
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int N = 5, K = 2;
// Function called
cout << MaximumSumOperations(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Utility function
static int MaximumSumOperations(int arr[], int N,
int K)
{
if (N == 1) {
return arr[0];
}
// Variable to count total operations
int total_division = 0;
// Perform the division operation on
// the elements multiple of K
for (int index = 0; index < N;
index++) {
if (arr[index] % K == 0) {
while (arr[index] > 1
&& arr[index] % K == 0) {
arr[index] = arr[index] / K;
total_division++;
}
}
}
Arrays.sort(arr);
// Update maximum element and
// decrement total_division
while (total_division > 0) {
arr[N - 1] = K * arr[N - 1];
total_division--;
}
int maximum_sum = 0;
for (int index = 0; index < N;
index++) {
maximum_sum += arr[index];
}
// Return maximum_sum
return maximum_sum;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5, K = 2;
// Function called
System.out.println(MaximumSumOperations(arr, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Utility function
def MaximumSumOperations(arr, N, K):
if (N == 1):
return arr[0];
# Variable to count total operations
total_division = 0;
# Perform the division operation on
# the elements multiple of K
for index in range(N):
if (arr[index] % K == 0):
while (arr[index] > 1 and arr[index] % K == 0):
arr[index] = arr[index] // K;
total_division += 1
arr.sort()
# Update maximum element and
# decrement total_division
while (total_division > 0):
arr[N - 1] = K * arr[N - 1];
total_division -= 1
maximum_sum = 0;
for index in range(N):
maximum_sum += arr[index];
# Return maximum_sum
return maximum_sum;
# Driver Code
arr = [1, 2, 3, 4, 5]
N = 5
K = 2
# Function called
print(MaximumSumOperations(arr, N, K));
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Utility function
static int MaximumSumOperations(int []arr, int N,
int K)
{
if (N == 1) {
return arr[0];
}
// Variable to count total operations
int total_division = 0;
// Perform the division operation on
// the elements multiple of K
for (int index = 0; index < N;
index++) {
if (arr[index] % K == 0) {
while (arr[index] > 1
&& arr[index] % K == 0) {
arr[index] = arr[index] / K;
total_division++;
}
}
}
Array.Sort(arr);
// Update maximum element and
// decrement total_division
while (total_division > 0) {
arr[N - 1] = K * arr[N - 1];
total_division--;
}
int maximum_sum = 0;
for (int index = 0; index < N;
index++) {
maximum_sum += arr[index];
}
// Return maximum_sum
return maximum_sum;
}
// Driver code
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5 };
int N = 5, K = 2;
// Function called
Console.Write(MaximumSumOperations(arr, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
46
时间复杂度: O(N * logN)
辅助空间: O(1)