📌  相关文章
📜  通过将子数组的所有元素除以 K 来最小化和

📅  最后修改于: 2021-10-26 06:01:16             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[]和一个正整数K ,任务是在执行给定操作最多一次后最小化数组元素的总和。操作是选择一个子数组并将子数组的所有元素除以K 。查找并打印可能的最小总和。
例子:

方法:

  • 使用 Kadane 的算法找到最大和子数组,比如maxSum,因为它将是有助于最大化数组总和的子数组。
  • 现在有两种情况:
    1. maxSum > 0:将找到的子数组的每个元素除以K ,所得数组的总和将尽可能最小。
    2. 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 现场工作专业课程学生竞争性编程现场课程