 1598263200

# Minimum decrements required such that sum of all adjacent pairs

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 ith element in the given array, the operations can be performed 0 to a[i] times. Since the answer can be large, compute it modulo 109 + 7.

Examples:

_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 by 2
• Reduce a by 7
• Reduce a by 11
• Reduce a 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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

Follow the steps below to solve the problem:

1. 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.
2. 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**.
1. Repeat the above steps for all the elements and print the number of operations calculated.

Below is the implementation of the above approach:

• C++
• Python3

`// 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);`

`cout << (minimum_required_operations(a, n, k));`

`**return**` `0;`

`}`

`// This code is contributed by chitranayal`

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.

#arrays #greedy #mathematical #searching

## Buddha Community  1598263200

## Minimum decrements required such that sum of all adjacent pairs

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 ith element in the given array, the operations can be performed 0 to a[i] times. Since the answer can be large, compute it modulo 109 + 7.

Examples:

_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 by 2
• Reduce a by 7
• Reduce a by 11
• Reduce a 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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

Follow the steps below to solve the problem:

1. 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.
2. 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**.
1. Repeat the above steps for all the elements and print the number of operations calculated.

Below is the implementation of the above approach:

• C++
• Python3

`// 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);`

`cout << (minimum_required_operations(a, n, k));`

`**return**` `0;`

`}`

`// This code is contributed by chitranayal`

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.

#arrays #greedy #mathematical #searching

1626849093

## MAGENTO 2 MINIMUM ORDER AMOUNT FOR CUSTOMER GROUP

Set a minimum order amount based on customer groups. Magento 2 Minimum Order Amount for Customer Group Extension helps the store owner to set a minimum order amount for better customer experience to encourage customers to purchase more products or buy bulk products from the store.

#magento 2 minimum order amount for customer group #minimum order amount for customer group #magento 2 #minimum order amount for customer group magento 2

1621318718

## Magento 2 minimum order amount for customer group

Cynoinfotech has developed Magento 2 Minimum Order Amount For Customer Group extension that enables restricting minimum order amount at checkout, based on customer groups as mandatory. The extension helps boost a store’s average order size.

#minimum order amount for customer group magento 2 extension #magento 2 minimum order amount for customer group #minimum order amount for customer group #magento 2 1594285500

## Find array elements equal to sum of any subarray of at least size 2

Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.

Examples:

Input:_ arr[] = {1, 2, 3, 4, 5, 6}_

Output:_ 3, 5, 6_

Explanation:

The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectively.

Input:_ arr[] = {5, 6, 10, 1, 3, 4, 8, 16}_

Output:_ 4, 8, 16_

Explanation:

The elements 4, 8, 16 are equal to the sum of subarrays {1, 3}, {1, 3, 4}, {1, 3, 4, 8} respectively

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use prefix sum array to solve the given problem. Create a prefix[] array that stores the prefix sum of all its preceding elements for every index. Store the sum of all subarrays in a map and search if any array element is present in the map or not.

#arrays #hash #prefix-sum #subarray #subarray-sum 1594238700

## Find array elements equal to sum of any subarray of at least size 2

Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.

Examples:

Input:_ arr[] = {1, 2, 3, 4, 5, 6}_

Output:_ 3, 5, 6_

Explanation:

The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectively.

Input:_ arr[] = {5, 6, 10, 1, 3, 4, 8, 16}_

Output:_ 4, 8, 16_

Explanation:

The elements 4, 8, 16 are equal to the sum of subarrays {1, 3}, {1, 3, 4}, {1, 3, 4, 8} respectively

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use prefix sum array to solve the given problem. Create a prefix[] array that stores the prefix sum of all its preceding elements for every index. Store the sum of all subarrays in a map and search if any array element is present in the map or not.

#arrays #hash #prefix-sum #subarray #subarray-sum