Given an array **arr[]** and an integer **K**, the task is to find the minimum number of operations required to change an array **B** of size **N** containing all zeros such that every element of B is greater than or equal to arr. i.e., arr[i] >= B[i]. In any operation, you can choose a subarray of B of size K and increment all the elements of the subarray by 1.**Examples:**

Input:arr[] = {1, 2, 3, 4, 5}, K = 2Output:9Explanation:

At first B[] = {0, 0, 0, 0, 0} operations = 0

Increment subarray a[1:2] by 1 => B = {1, 1, 0, 0, 0}, operations = 1

Increment subarray a[2:3] by 1 => B = {1, 2, 1, 0, 0}, operations = 2

Increment subarray a[3:4] by 2 => B = {1, 2, 3, 2, 0}, operations = 4

Increment subarray a[4:5] by 5 => B = {1, 2, 3, 7, 5}, operations = 9

Therefore, count of such operations required is 9.Input:arr[] = {2, 3, 1}, K = 3Output:3Explanation:

Incrementing the entire array by 3

**Approach:** The idea is to increment the subarray of size K whenever there is a B[i] is less than arr[i] and also increment the count of such operations by 1 at each step. To increment the subarray of size K use the Difference array for Range Query update in O(1).

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// minimum number of operations ` `// required to change an array of` `// all zeros such that every element` `// is greater than the given array` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum ` `// number of operations required` `// to change all the array of zeros` `// such that every element is greater` `// than the given array` `int` `find_minimum_operations(` `int` `n, ` `int` `b[],` ` ` `int` `k)` `{` ` ` ` ` `// Declaring the difference` ` ` `// array of size N` ` ` `int` `d[n + 1] = {0};` ` ` `// Number of operations` ` ` `int` `operations = 0, need;` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` ` ` `// First update the D[i] value ` ` ` `// with the previous value` ` ` `if` `(i > 0)` ` ` `{` ` ` `d[i] += d[i - 1];` ` ` `}` ` ` ` ` `// The index i has to be incremented` ` ` `if` `(b[i] > d[i])` ` ` `{` ` ` ` ` `// We have to perform ` ` ` `// (b[i]-d[i]) operations more` ` ` `operations += b[i] - d[i];` ` ` `need = b[i] - d[i];` ` ` `// Increment the range` ` ` `// i to i + k by need` ` ` `d[i] += need;` ` ` `// Check if i + k is valid index` ` ` `if` `(i + k <= n)` ` ` `{` ` ` `d[i + k]-= need;` ` ` `}` ` ` `}` ` ` `}` ` ` `cout << operations << endl;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `n = 5;` ` ` `int` `b[] = { 1, 2, 3, 4, 5 };` ` ` `int` `k = 2;` ` ` ` ` `// Function Call` ` ` `find_minimum_operations(n, b, k);` ` ` ` ` `return` `0;` `}` `// This code is contributed by shubhamsingh10` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// minimum number of operations ` `// required to change an array of` `// all zeros such that every element` `// is greater than the given array` `class` `GFG{` `// Function to find the minimum ` `// number of operations required` `// to change all the array of zeros` `// such that every element is greater` `// than the given array` `static` `void` `find_minimum_operations(` `int` `n, ` `int` `b[],` ` ` `int` `k)` `{` ` ` ` ` `// Declaring the difference` ` ` `// array of size N` ` ` `int` `d[] = ` `new` `int` `[n + ` `1` `];` ` ` `// Number of operations` ` ` `int` `i, operations = ` `0` `, need;` ` ` `for` `(i = ` `0` `; i < n; i++)` ` ` `{` ` ` ` ` `// First update the D[i] value ` ` ` `// with the previous value` ` ` `if` `(i > ` `0` `)` ` ` `{` ` ` `d[i] += d[i - ` `1` `];` ` ` `}` ` ` ` ` `// The index i has to be incremented` ` ` `if` `(b[i] > d[i])` ` ` `{` ` ` ` ` `// We have to perform ` ` ` `// (b[i]-d[i]) operations more` ` ` `operations += b[i] - d[i];` ` ` `need = b[i] - d[i];` ` ` `// Increment the range` ` ` `// i to i + k by need` ` ` `d[i] += need;` ` ` `// Check if i + k is valid index` ` ` `if` `(i + k <= n)` ` ` `{` ` ` `d[i + k]-= need;` ` ` `}` ` ` `}` ` ` `}` ` ` `System.out.println(operations);` `}` `// Driver Code` `public` `static` `void` `main (String []args)` `{` ` ` `int` `n = ` `5` `;` ` ` `int` `b[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `};` ` ` `int` `k = ` `2` `;` ` ` ` ` `// Function Call` ` ` `find_minimum_operations(n, b, k);` `}` `}` `// This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# minimum number of operations required ` `# to change an array of all zeros ` `# such that every element is greater than` `# the given array` `# Function to find the minimum ` `# number of operations required` `# to change all the array of zeros` `# such that every element is greater` `# than the given array` `def` `find_minimum_operations(n, b, k):` ` ` `# Declaring the difference` ` ` `# array of size N` ` ` `d ` `=` `[` `0` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` `# Number of operations` ` ` `operations ` `=` `0` ` ` `for` `i ` `in` `range` `(n):` ` ` `# First update the D[i] value with` ` ` `# the previous value` ` ` `d[i]` `+` `=` `d[i` `-` `1` `]` ` ` `# The index i has to be incremented` ` ` `if` `b[i]>d[i]:` ` ` `# We have to perform ` ` ` `# (b[i]-d[i]) operations more` ` ` `operations` `+` `=` `(b[i]` `-` `d[i])` ` ` `need ` `=` `(b[i]` `-` `d[i]) ` ` ` `# Increment the range` ` ` `# i to i + k by need` ` ` `d[i]` `+` `=` `need` ` ` `# Check if i + k is valid index` ` ` `if` `i ` `+` `k<` `=` `n:` ` ` `d[i ` `+` `k]` `-` `=` `need` ` ` `return` `operations` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `n ` `=` `5` ` ` `b ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `]` ` ` `k ` `=` `2` ` ` ` ` `# Function Call` ` ` `print` `(find_minimum_operations(n, b, k))` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// minimum number of operations ` `// required to change an array of` `// all zeros such that every element` `// is greater than the given array` `using` `System;` `class` `GFG{` ` ` `// Function to find the minimum ` `// number of operations required` `// to change all the array of zeros` `// such that every element is greater` `// than the given array` `static` `void` `find_minimum_operations(` `int` `n, ` `int` `[] b,` ` ` `int` `k)` `{` ` ` ` ` `// Declaring the difference` ` ` `// array of size N` ` ` `int` `[] d = ` `new` `int` `[n + 1];` ` ` ` ` `// Number of operations` ` ` `int` `i, operations = 0, need;` ` ` ` ` `for` `(i = 0; i < n; i++)` ` ` `{` ` ` ` ` `// First update the D[i] value ` ` ` `// with the previous value` ` ` `if` `(i > 0)` ` ` `{` ` ` `d[i] += d[i - 1];` ` ` `}` ` ` ` ` `// The index i has to be incremented` ` ` `if` `(b[i] > d[i])` ` ` `{` ` ` ` ` `// We have to perform ` ` ` `// (b[i]-d[i]) operations more` ` ` `operations += b[i] - d[i];` ` ` ` ` `need = b[i] - d[i];` ` ` ` ` `// Increment the range` ` ` `// i to i + k by need` ` ` `d[i] += need;` ` ` ` ` `// Check if i + k is valid index` ` ` `if` `(i + k <= n)` ` ` `{` ` ` `d[i + k]-= need;` ` ` `}` ` ` `}` ` ` `}` ` ` `Console.Write(operations);` `}` ` ` `// Driver Code` `public` `static` `void` `Main (` `string` `[]args)` `{` ` ` `int` `n = 5;` ` ` `int` `[] b = { 1, 2, 3, 4, 5 };` ` ` `int` `k = 2;` ` ` ` ` `// Function Call` ` ` `find_minimum_operations(n, b, k);` `}` `}` ` ` `// This code is contributed by rock_cool` |

*chevron_right*

*filter_none*

**Output:**

9

**Time Complexity: O(N)**

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.

## Recommended Posts:

- Minimum cost required to convert all Subarrays of size K to a single element
- Minimum increment or decrement required to sort the array | Top-down Approach
- Minimum increment or decrement operations required to make the array sorted
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Minimum increment in the sides required to get non-negative area of a triangle
- Minimum increment/decrement operations required to make Median as X
- Minimum count of numbers required from given array to represent S
- Count minimum factor jumps required to reach the end of an Array
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Minimum and Maximum of all subarrays of size K using Map
- Sum of minimum and maximum elements of all subarrays of size k.
- Min difference between maximum and minimum element in all Y size subarrays
- Count of subarrays of size K with elements having even frequencies
- Count of subarrays of size K which is a permutation of numbers from 1 to K
- Count non decreasing subarrays of size N from N Natural numbers
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Find an array of size N having exactly K subarrays with sum S
- Minimum Increment operations to make Array unique
- Minimum range increment operations to Sort an array
- Minimum increment operations to make the array in increasing order

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.