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

📅  最后修改于: 2021-09-06 05:39:13             🧑  作者: Mango

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

例子:

方法:

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

  1. 迭代数组a[]并对每个相邻对,检查它们的总和是否小于或等于K。如果发现为真,则不需要更改。
  2. 对于总和大于K 的对,请按照以下步骤操作:
    • 如果 pair 的第一个元素超过K ,则使 pair 中第一个元素的值等于K 。增加第一个元素的值– K所需的操作次数,并将该对的第一个元素的值更新为K
    • 现在,第二个元素应用pair – K运算的和以确保pair 的和等于K ,并将pair 的第二个元素更新为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


Javascript


输出:
178

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live