-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adding Ones.cpp
65 lines (55 loc) · 1.07 KB
/
Adding Ones.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
56
57
58
59
60
61
62
63
64
65
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
/*
Prefix sum approach is used in this problem
Updates = {1,1,2,3} ... 1 based indexing
a[] = {0,0,0}
for updates[0] => 1
a[] = {1,0,0}
for updates[1] => 1
a[] = {2,0,0}
for updates[2] => 2
a[] = {2,1,0}
for updates[3] => 3
a[] = {2,1,1}
after second for loop,
a[] = {2,3,4}
*/
class Solution
{
public:
void update(int a[], int n, int updates[], int k)
{
for (int i = 0; i < k; i++)
{
a[updates[i] - 1]++;
}
for (int i = 1; i < n; i++)
{
a[i] += a[i - 1];
}
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
int a[n] = {0}, updates[k] = {0};
for (int i = 0; i < k; i++)
cin >> updates[i];
Solution ob;
ob.update(a, n, updates, k);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
return 0;
}
// } Driver Code Ends