给定一个由N 个正整数组成的数组a[]和一个整数K ,任务是找到使相邻元素之和小于或等于K所需的最少操作次数,其中一个操作涉及减少任何数组element by 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 的对,请按照以下步骤操作:
- 如果 pair 的第一个元素超过K ,则使 pair 中第一个元素的值等于K 。增加第一个元素的值– K所需的操作次数,并将该对的第一个元素的值更新为K 。
- 现在,对第二个元素应用pair – K运算的和以确保pair 的和等于K ,并将pair 的第二个元素更新为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
Javascript
178
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。