Skip to content

Commit cfa4d43

Browse files
authored
Create 1508. Range Sum of Sorted Subarray Sums (#546)
2 parents 5acc166 + d55acd1 commit cfa4d43

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
const int mod = 1e9+7;
3+
public:
4+
int rangeSum(vector<int>& nums, int n, int l, int r) {
5+
vector<int> arr;
6+
7+
// Generate all subarray sums
8+
for(int i = 0; i < n; i++) {
9+
int sum = 0;
10+
for(int j = i; j < n; j++) {
11+
sum = (sum + nums[j]) % mod;
12+
arr.push_back(sum);
13+
}
14+
}
15+
16+
// Sort the subarray sums
17+
sort(arr.begin(), arr.end());
18+
19+
// Compute the range sum
20+
int sum = 0;
21+
for(int i = l - 1; i < r; i++) {
22+
sum = (sum + arr[i]) % mod;
23+
}
24+
25+
return sum;
26+
}
27+
};

0 commit comments

Comments
 (0)