-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.java
51 lines (36 loc) · 1.21 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
public class test {
public static void main(String args[]) {
//declare a scanner to take input
Scanner scan = new Scanner(System.in);
//take input of the number of numbers and number of operations
long n = scan.nextInt(), q = scan.nextInt();
//declare a prefix sum array
long pre[] = new long[(int)n+2];
//Idea here is that if we are given l, r, val then we will add val to pre[l] and subtract val from pre[r+1];
//At the end, we will run a loop and find the prefix sums, and that will actually be the final array
for(int i = 0; i < q; i++) {
int l, r, val;
//take input of l, r, val;
l = scan.nextInt();
r = scan.nextInt();
val = scan.nextInt();
//add val to pre[l];
pre[l] += val;
//subtract val from pre[r+1];
pre[r+1] -= val;
}
//find prefix sum of this array
for(int i = 2; i <= n; i++) {
pre[i] += pre[i-1];
}
//initialise answer to 0
long ans = 0;
//iterate over the array and keep updating answer
for(int i = 1; i <= n; i++) {
ans = Math.max(ans, pre[i]);
}
//print the answer
System.out.println(ans);
}
}