-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
37 lines (29 loc) · 801 Bytes
/
Copy pathsolution.cpp
File metadata and controls
37 lines (29 loc) · 801 Bytes
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
class Solution
{
public:
vector<int> findMean(vector<int> &arr, vector<vector<int>> &queries)
{
int n = arr.size();
// Prefix sum array
vector<long long> prefix(n + 1, 0);
// Build prefix sum
for (int i = 0; i < n; i++)
{
prefix[i + 1] = prefix[i] + arr[i];
}
vector<int> ans;
// Process each query
for (auto &q : queries)
{
int l = q[0];
int r = q[1];
// Sum of subarray [l...r]
long long sum = prefix[r + 1] - prefix[l];
// Number of elements in range
int len = r - l + 1;
// Floor mean
ans.push_back(sum / len);
}
return ans;
}
};