📌  相关文章
📜  使数组中所有相邻对的总和不超过K所需的最小减量

📅  最后修改于: 2021-05-18 01:09:30             🧑  作者: Mango

给定一个由N个正整数和一个整数K组成的数组a [] ,任务是找到使相邻元素的总和小于或等于K所需的最小操作数,其中,一个操作涉及减少任何数组元素乘以1。对于给定数组中的每个i元素,可以执行0a [i]次操作。由于答案可能很大,因此请以10 9 + 7为模进行计算。

例子:

方法:

请按照以下步骤解决问题:

  1. 遍历数组a [] ,对于每个相邻对,检查它们的总和是否小于或等于K。如果发现为真,则不需要进行任何更改。
  2. 对于总和大于K的对,请执行以下步骤:
    • 如果对中的第一个元素超过K ,则使对中的第一个元素的值等于K。增加第一个元素– K的值所需的操作数,并将该对中第一个元素的值更新为K。
    • 现在,在第二个元素上应用对– K运算之和,以确保对的总和等于K ,并将对的第二个元素更新为第一个元素的K –值
  3. 对所有元素重复上述步骤,并打印计算出的操作数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
// Function to calculate the minimum
// number of operations required
int minimum_required_operations(int arr[],
                                int n, int k)
{
     
    // Stores the total number
    // of operations
    int answer = 0;
 
    long long mod = 1000000007;
 
    // Iterate over the array
    for(int i = 0; i < n - 1; i++)
    {
         
        // If the sum of pair of adjacent
        // elements exceed k.
        if (arr[i] + arr[i + 1] > k)
        {
             
            // If current element exceeds k
            if (arr[i] > k)
            {
                 
                // Reduce arr[i] to k
                answer += (arr[i] - k);
                arr[i] = k;
            }
             
            // Update arr[i + 1] accordingly
            answer += (arr[i] + arr[i + 1]) - k;
            arr[i + 1] = (k - arr[i]);
 
            // Update answer
            answer %= mod;
        }
    }
    return answer;
}
 
// Driver Code
int main()
{
    int a[] = { 9, 50, 4, 14, 42, 89 };
    int k = 10;
    int n = sizeof(a) / sizeof(a[0]);
     
    cout << (minimum_required_operations(a, n, k));
     
    return 0;
}
 
// This code is contributed by chitranayal


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to calculate the minimum
// number of operations required
static int minimum_required_operations(int arr[],
                                       int n, int k)
{
     
    // Stores the total number
    // of operations
    int answer = 0;
 
    long mod = 1000000007;
 
    // Iterate over the array
    for(int i = 0; i < n - 1; i++)
    {
         
        // If the sum of pair of adjacent
        // elements exceed k.
        if (arr[i] + arr[i + 1] > k)
        {
             
            // If current element exceeds k
            if (arr[i] > k)
            {
                 
                // Reduce arr[i] to k
                answer += (arr[i] - k);
                arr[i] = k;
            }
             
            // Update arr[i + 1] accordingly
            answer += (arr[i] + arr[i + 1]) - k;
            arr[i + 1] = (k - arr[i]);
 
            // Update answer
            answer %= mod;
        }
    }
    return answer;
}
 
// Driver Code
public static void main(String[] args)
{
    int a[] = { 9, 50, 4, 14, 42, 89 };
    int k = 10;
    int n = a.length;
     
    System.out.print(
        minimum_required_operations(a, n, k));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 Program to implement
# the above approach
 
# Function to calculate the minimum
# number of operations required
def minimum_required_operations(arr, n, k):
 
    # Stores the total number
    # of operations
    answer = 0
 
    mod = 10 ** 9 + 7
 
    # Iterate over the array
    for i in range(n - 1):
 
        # If the sum of pair of adjacent
        # elements exceed k.
        if arr[i] + arr[i + 1] > k:
 
            # If current element exceeds k
            if arr[i] > k:
               
              # Reduce arr[i] to k
                answer += (arr[i] - k)
                arr[i] = k
 
            # Update arr[i + 1] accordingly
            answer += (arr[i] + arr[i + 1]) - k
            arr[i + 1] = (k - arr[i])
 
            # Update answer
            answer %= mod
             
    return answer
 
 
# Driver Code
 
a = [9, 50, 4, 14, 42, 89]
k = 10
 
print(minimum_required_operations(a, len(a), k))


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
  // Function to calculate the minimum
  // number of operations required
  static int minimum_required_operations(int[] arr, int n,
                                         int k)
  {
 
    // Stores the total number
    // of operations
    int answer = 0;
 
    long mod = 1000000007;
 
    // Iterate over the array
    for (int i = 0; i < n - 1; i++)
    {
 
      // If the sum of pair of adjacent
      // elements exceed k.
      if (arr[i] + arr[i + 1] > k)
      {
 
        // If current element exceeds k
        if (arr[i] > k)
        {
 
          // Reduce arr[i] to k
          answer += (arr[i] - k);
          arr[i] = k;
        }
 
        // Update arr[i + 1] accordingly
        answer += (arr[i] + arr[i + 1]) - k;
        arr[i + 1] = (k - arr[i]);
 
        // Update answer
        answer = (int)(answer % mod);
      }
    }
    return answer;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int[] a = { 9, 50, 4, 14, 42, 89 };
    int k = 10;
    int n = a.Length;
 
    Console.Write(minimum_required_operations(a, n, k));
  }
}
 
// This code is contributed by gauravrajput1


输出:
178


时间复杂度: O(N)
辅助空间: O(1)