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

📅  最后修改于: 2021-04-22 05:59:34             🧑  作者: Mango

给定一个由N个整数和一个正整数K组成的数组arr [] ,任务是在至少执行一次给定操作后,最小化数组元素的总和。该操作是选择一个子数组,并将该子数组的所有元素除以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


输出:
0.5

时间复杂度: O(N)