Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.
For example, the length of your array of zeros. Your list of queries is as follows:
a b k
1 5 3
4 8 7
6 9 1
Add the values of k
between the indices a
and b
inclusive:
1 2 3 4 5 6 7 8 9 10
[0,0,0, 0, 0,0,0,0,0, 0]
[3,3,3, 3, 3,0,0,0,0, 0]
[3,3,3,10,10,7,7,7,0, 0]
[3,3,3,10,10,8,8,8,1, 0]
The largest value is 10
after all operations are performed.
Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array.
arrayManipulation
has the following parameters:
n
- the number of elements in your array
queries
- a two dimensional array of queries where each queries[i]
contains three integers, a
, b
, and k
.
The first line contains two space-separated integers m
and 'n
, the size of the array and the number of operations.
Each of the next m
lines contains three space-separated integers a
, b
, and k
, the left index, right index and summand.
...
Return the integer maximum value in the finished array.
5 3
1 2 100
2 5 100
3 4 100
200
After the first update list will be 100 100 0 0 0
After the second update list will be 100 200 100 100 100
After the third update list will be 100 200 200 200 100
The required answer will be 200
import java.util.Arrays;
/**
* https://www.geeksforgeeks.org/constant-time-range-add-operation-array/
*/
public class RangeFillOperation {
static long rangeFillOperation(int n, int[][] queries) {
long[] diff = new long[n];
long[] arr;
for (int i = 0; i < queries.length; i++) {
int a = queries[i][0] - 1;
int b = queries[i][1] - 1;
int k = queries[i][2];
diff[a] += k;
if (b + 1 != n) diff[b + 1] -= k;
}
arr = Arrays.copyOf(diff, diff.length);
for (int i = 1; i < n; i++)
arr[i] += arr[i - 1];
return Arrays.stream(arr).max().getAsLong();
}
}