-
Notifications
You must be signed in to change notification settings - Fork 709
Expand file tree
/
Copy pathPre_com_using_prefixSum_1d2dArray.cpp
More file actions
64 lines (57 loc) · 1.74 KB
/
Pre_com_using_prefixSum_1d2dArray.cpp
File metadata and controls
64 lines (57 loc) · 1.74 KB
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
/*
Given array a of N integers , Given Q queries and in each query given L and r.
Print sum of array element from index L to R ( L , R included).
Constraints.
1 <= N <= 10^5
1 <= a[i] <= 10^9
1 <= Q <= 10^5
1 <= L , R <= N
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int arr[N];
int prefix_sum[N]; // array for
int main()
{
// int n;
// cin>>n;
// for (int i = 1; i <=n; i++)
// {
// cin >> arr[i];
// }
// int q;
// cin>>q;
// while (q--)
// {
// int l, r;
// cin >> l >> r;
// long long sum = 0;
// for (int i = l; i <= r; i++) // Worst case time complexity o(N) + o(Q) = o(N^2)
// {
// sum = sum + arr[i];
// }
// cout << sum << endl;
// }
// time complexity is o(N^2) and code will also not run for 10^5 input/ second. so we will sum before computation.
// In pre_computation prefix sum technique we use to store the sum of first i element.
// suppose we are at index 3 then we will also have store the sum of index 1 , 2 and 3 .
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> arr[i];
prefix_sum[i] = prefix_sum[ i - 1] + arr[i];
}
int q;
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r;
cout << prefix_sum[r] - prefix_sum[l-1] << endl;
}
// Now the time complexity is o(N).
// Here we use a array in which we store sum of ith element . and for finding sum l to r we just minus r - l-1.
return 0;
}