# Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K

Given an array **a[]** consisting of **N** positive integers, and an integer **K**, the task is to find the minimum number of operations required to make the sum of adjacent elements less than or equal to **K**, where, one operation involves decreasing any array element by 1. For every **i ^{th}** element in the given array, the operations can be performed

**0**to

**a[i]**times. Since the answer can be large, compute it modulo

**10**.

^{9}+ 7**Examples:**

Input:a[] = {11, 3, 13, 10, 8, 17, 22}, K = 14Output:34Explanation:

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 = 0Output3:999999979Explanation:

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 % (10^{9}+ 7).

Sum of array is 4000000000

Therefore, the required answer is 4000000000 % 10^{9}+ 7 = 999999979

**Approach:**

Follow the steps below to solve the problem:

- Iterate over the array
**a[]**and for each adjacent pair, check if their sum is less than or equal to**K.**If found to be true, no changes are required. - For pairs with sum greater than
**K,**follow the steps below:- If the first element of pair exceeds
**K**, make the value of the first element in the pair equal to**K**. Increase the number of operations required by**value of first element – K**and update the value of the first element of the pair to**K**. - Now, apply the
**sum of pair – K**operations on the second element to ensure that the sum of pair is equal to**K**, and update the second element of pair to**K – value of first element**.

- If the first element of pair exceeds
- Repeat the above steps for all the elements and print the number of operations calculated.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include<bits/stdc++.h>` `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

`<script>` `// Java Script program to implement` `// the above approach` `// Function to calculate the minimum` `// number of operations required` `function` `minimum_required_operations(arr,n,k)` `{` ` ` ` ` `// Stores the total number` ` ` `// of operations` ` ` `let answer = 0;` ` ` `let mod = 1000000007;` ` ` `// Iterate over the array` ` ` `for` `(let 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` ` ` `let a = [ 9, 50, 4, 14, 42, 89 ];` ` ` `let k = 10;` ` ` `let n = a.length;` ` ` ` ` `document.write(` ` ` `minimum_required_operations(a, n, k));` `// This code is contributed by sravan kumar Gottumukkala` `</script>` |

**Output:**

178

**Time Complexity: **O(N)**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.