# Array Manipulation Java Solution

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 between the indices and inclusive:

```
index-> 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 after all operations are performed.

**Function Description**

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*.

**Input Format**

The first line contains two space-separated integers and , the size of the array and the number of operations.

Each of the next lines contains three space-separated integers , and , the left index, right index and summand.

**Constraints**

**Output Format**

Return the integer maximum value in the finished array.

**Sample Input**

```
5 3
1 2 100
2 5 100
3 4 100
```

**Sample Output**

```
200
```

**Explanation**

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 .

## Solution in Java

**Approach 1**

```
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long size = scanner.nextLong();
Map<Long, Long> map = new HashMap<>();
long operations = scanner.nextLong();
for (long i = 0; i < operations; i++) {
long start = scanner.nextLong();
long end = scanner.nextLong();
long value = scanner.nextLong();
map.put(start, (map.containsKey(start) ? map.get(start) : 0) + value);
map.put(end + 1, (map.containsKey(end + 1) ? map.get(end + 1) : 0) - value);
}
long max = 0;
long value = 0;
for (long i = 0; i < size; i++) {
value += (map.containsKey(i + 1) ? map.get(i + 1) : 0);
max = Math.max(max, value);
}
System.out.println(max);
}
}
```

**Approach 2**

```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
//for index 0 as dummy
long[] numbers = new long[N+1];
for(int i = 0; i < M; i++){
int a = in.nextInt();
int b = in.nextInt();
int k = in.nextInt();
numbers[a] += k;
if(b + 1 <= N){
numbers[b+1] -= k;
}
}
long max = Long.MIN_VALUE;
long sum = 0;
for(int i = 1; i < numbers.length; i++){
sum = sum + numbers[i];
if(sum > max){
max = sum;
}
}
System.out.println(max);
}
}
```