给定一个由N 个整数组成的数组arr[]和一个正整数K ,任务是在执行给定操作最多一次后最小化数组元素的总和。操作是选择一个子数组并将子数组的所有元素除以K 。查找并打印可能的最小总和。
例子:
Input: arr[] = {1, -2, 3}, K = 2
Output: 0.5
Choose the subarray {3} and divide them by K
The array becomes {1, -2, 1.5} where 1 – 2 + 1.5 = 0.5
Input: arr[] = {-1, -2, -3, -5}, K = 4
Output: -11
There is no need to perform the operation as the
sum of the array elements is already minimum.
方法:
- 使用 Kadane 的算法找到最大和子数组,比如maxSum,因为它将是有助于最大化数组总和的子数组。
- 现在有两种情况:
- maxSum > 0:将找到的子数组的每个元素除以K ,所得数组的总和将尽可能最小。
- maxSum ≤ 0:数组的总和已经是最小值,不需要进行运算。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum subarray sum
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
// Function to return the minimized sum
// of the array elements after performing
// the given operation
double minimizedSum(int a[], int n, int K)
{
// Find maximum subarray sum
int sum = maxSubArraySum(a, n);
double totalSum = 0;
// Find total sum of the array
for (int i = 0; i < n; i++)
totalSum += a[i];
// Maximum subarray sum is already negative
if (sum < 0)
return totalSum;
// Choose the subarray whose sum is
// maximum and divide all elements by K
totalSum = totalSum - sum + (double)sum / (double)K;
return totalSum;
}
// Driver code
int main()
{
int a[] = { 1, -2, 3 };
int n = sizeof(a) / sizeof(a[0]);
int K = 2;
cout << minimizedSum(a, n, K);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the maximum subarray sum
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
// Function to return the minimized sum
// of the array elements after performing
// the given operation
static double minimizedSum(int a[], int n, int K)
{
// Find maximum subarray sum
int sum = maxSubArraySum(a, n);
double totalSum = 0;
// Find total sum of the array
for (int i = 0; i < n; i++)
totalSum += a[i];
// Maximum subarray sum is already negative
if (sum < 0)
return totalSum;
// Choose the subarray whose sum is
// maximum and divide all elements by K
totalSum = totalSum - sum + (double)sum /
(double)K;
return totalSum;
}
// Driver code
public static void main(String []args)
{
int a[] = { 1, -2, 3 };
int n = a.length;
int K = 2;
System.out.println(minimizedSum(a, n, K));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
import sys
# Function to return the maximum subarray sum
def maxSubArraySum(a, size) :
max_so_far = -(sys.maxsize - 1);
max_ending_here = 0;
for i in range(size) :
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here) :
max_so_far = max_ending_here;
if (max_ending_here < 0) :
max_ending_here = 0;
return max_so_far;
# Function to return the minimized sum
# of the array elements after performing
# the given operation
def minimizedSum(a, n, K) :
# Find maximum subarray sum
sum = maxSubArraySum(a, n);
totalSum = 0;
# Find total sum of the array
for i in range(n) :
totalSum += a[i];
# Maximum subarray sum is already negative
if (sum < 0) :
return totalSum;
# Choose the subarray whose sum is
# maximum and divide all elements by K
totalSum = totalSum - sum + sum / K;
return totalSum;
# Driver code
if __name__ == "__main__" :
a = [ 1, -2, 3 ];
n = len(a);
K = 2;
print(minimizedSum(a, n, K));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum subarray sum
static int maxSubArraySum(int []a, int size)
{
int max_so_far = int.MinValue,
max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
// Function to return the minimized sum
// of the array elements after performing
// the given operation
static double minimizedSum(int []a, int n, int K)
{
// Find maximum subarray sum
int sum = maxSubArraySum(a, n);
double totalSum = 0;
// Find total sum of the array
for (int i = 0; i < n; i++)
totalSum += a[i];
// Maximum subarray sum is already negative
if (sum < 0)
return totalSum;
// Choose the subarray whose sum is
// maximum and divide all elements by K
totalSum = totalSum - sum + (double)sum /
(double)K;
return totalSum;
}
// Driver code
public static void Main(String []args)
{
int []a = { 1, -2, 3 };
int n = a.Length;
int K = 2;
Console.WriteLine(minimizedSum(a, n, K));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
0.5
时间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。