Minimum count of increment of K size subarrays required to form a given Array
Last Updated :
16 Aug, 2021
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 = 2
Output: 9
Explanation:
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 = 3
Output: 3
Explanation:
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++
#include <bits/stdc++.h>
using namespace std;
int find_minimum_operations( int n, int b[],
int k)
{
int d[n + 1] = {0};
int operations = 0, need;
for ( int i = 0; i < n; i++)
{
if (i > 0)
{
d[i] += d[i - 1];
}
if (b[i] > d[i])
{
operations += b[i] - d[i];
need = b[i] - d[i];
d[i] += need;
if (i + k <= n)
{
d[i + k]-= need;
}
}
}
cout << operations << endl;
}
int main()
{
int n = 5;
int b[] = { 1, 2, 3, 4, 5 };
int k = 2;
find_minimum_operations(n, b, k);
return 0;
}
|
Java
class GFG{
static void find_minimum_operations( int n, int b[],
int k)
{
int d[] = new int [n + 1 ];
int i, operations = 0 , need;
for (i = 0 ; i < n; i++)
{
if (i > 0 )
{
d[i] += d[i - 1 ];
}
if (b[i] > d[i])
{
operations += b[i] - d[i];
need = b[i] - d[i];
d[i] += need;
if (i + k <= n)
{
d[i + k]-= need;
}
}
}
System.out.println(operations);
}
public static void main (String []args)
{
int n = 5 ;
int b[] = { 1 , 2 , 3 , 4 , 5 };
int k = 2 ;
find_minimum_operations(n, b, k);
}
}
|
Python3
def find_minimum_operations(n, b, k):
d = [ 0 for i in range (n + 1 )]
operations = 0
for i in range (n):
d[i] + = d[i - 1 ]
if b[i]>d[i]:
operations + = (b[i] - d[i])
need = (b[i] - d[i])
d[i] + = need
if i + k< = n:
d[i + k] - = need
return operations
if __name__ = = "__main__" :
n = 5
b = [ 1 , 2 , 3 , 4 , 5 ]
k = 2
print (find_minimum_operations(n, b, k))
|
C#
using System;
class GFG{
static void find_minimum_operations( int n, int [] b,
int k)
{
int [] d = new int [n + 1];
int i, operations = 0, need;
for (i = 0; i < n; i++)
{
if (i > 0)
{
d[i] += d[i - 1];
}
if (b[i] > d[i])
{
operations += b[i] - d[i];
need = b[i] - d[i];
d[i] += need;
if (i + k <= n)
{
d[i + k]-= need;
}
}
}
Console.Write(operations);
}
public static void Main ( string []args)
{
int n = 5;
int [] b = { 1, 2, 3, 4, 5 };
int k = 2;
find_minimum_operations(n, b, k);
}
}
|
Javascript
<script>
function find_minimum_operations(n, b, k)
{
let d = new Array(n + 1);
d.fill(0);
let i, operations = 0, need;
for (i = 0; i < n; i++)
{
if (i > 0)
{
d[i] += d[i - 1];
}
if (b[i] > d[i])
{
operations += b[i] - d[i];
need = b[i] - d[i];
d[i] += need;
if (i + k <= n)
{
d[i + k]-= need;
}
}
}
document.write(operations);
}
let n = 5;
let b = [ 1, 2, 3, 4, 5 ];
let k = 2;
find_minimum_operations(n, b, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...