-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathArray_Manipulation.cpp
56 lines (50 loc) · 1.21 KB
/
Array_Manipulation.cpp
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
52
53
54
55
/*
Problem link : https://www.hackerrank.com/challenges/crush/problem
Problem description:
Starting with a 1-indexed array of zeros and a list of operations, for each query add a value to each the array element between two given indices, inclusive.
Once all operations have been performed, return the maximum value in the array.
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
void Max_Value(int n, int m)
{
int a, b, k;
vector<ll> prefix_sum(n + 1, 0);
for (ll queries = 1; queries <= m; queries++)
{
cin >> a >> b >> k;
// Adding k at the starting point of the query
prefix_sum[a] += k;
// Subtracting K at the position after end point of query if and only if, it is valid.
if ((b + 1) <= n)
{
prefix_sum[b + 1] -= k;
}
}
ll answer = INT_MIN;
for (ll index = 1; index <= n; index++)
{
prefix_sum[index] += prefix_sum[index - 1];
answer = max(answer, prefix_sum[index]);
}
cout << answer << endl;
}
int main()
{
ll n, m;
cin >> n >> m;
// Max_value function prints the required answer after performing all queries.
Max_Value(n, m);
}
/*
Time Complexity : O(n)
Space Complexity : O(n)
Input:
5 3
1 2 100
2 5 100
3 4 100
Output:
200
*/