给定一个由N个正整数和一个整数K组成的数组a [] ,任务是找到使相邻元素的总和小于或等于K所需的最小操作数,其中,一个操作涉及减少任何数组元素乘以1。对于给定数组中的每个第i个元素,可以执行0到a [i]次操作。由于答案可能很大,因此请以10 9 + 7为模进行计算。
例子:
Input: a[] = {11, 3, 13, 10, 8, 17, 22}, K = 14
Output: 34
Explanation:
Minimum number of operations required to obtain the desired arrangement is as follows:
- Reduce a[2] by 2
- Reduce a[3] by 7
- Reduce a[5] by 11
- Reduce a[6] by 14
The given array is modified to the following arrangement = {11, 3, 11, 3, 8, 6, 8}
Total number of operations is 2 + 5 + 7 + 11 + 18 = 34.
Input: a[] = {1000000000, 1000000000, 1000000000, 1000000000}, K = 0
Output3: 999999979
Explanation:
Since the sum of adjacent pairs is required to be 0, all elements in the array need to be reduced to 0.
Therefore, the answer is sum of array % (109 + 7).
Sum of array is 4000000000
Therefore, the required answer is 4000000000 % 109 + 7 = 999999979
方法:
请按照以下步骤解决问题:
- 遍历数组a [] ,对于每个相邻对,检查它们的总和是否小于或等于K。如果发现为真,则不需要进行任何更改。
- 对于总和大于K的对,请执行以下步骤:
- 如果对中的第一个元素超过K ,则使对中的第一个元素的值等于K。增加第一个元素– K的值所需的操作数,并将该对中第一个元素的值更新为K。
- 现在,在第二个元素上应用对– K运算之和,以确保对的总和等于K ,并将对的第二个元素更新为第一个元素的K –值。
- 对所有元素重复上述步骤,并打印计算出的操作数。
下面是上述方法的实现:
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)