-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangeSum.java
More file actions
27 lines (22 loc) · 836 Bytes
/
rangeSum.java
File metadata and controls
27 lines (22 loc) · 836 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
class Solution {
public int rangeSum(int[] nums, int n, int left, int right) {
int MOD = 1_000_000_007;
List<Integer> subarraySums = new ArrayList<>();
// Step 1: Generate all subarray sums
for (int i = 0; i < n; i++) {
int currentSum = 0;
for (int j = i; j < n; j++) {
currentSum += nums[j];
subarraySums.add(currentSum);
}
}
// Step 2: Sort the subarray sums
Collections.sort(subarraySums);
// Step 3: Calculate the sum from index `left` to `right` (1-based index)
long rangeSum = 0;
for (int i = left - 1; i < right; i++) {
rangeSum = (rangeSum + subarraySums.get(i)) % MOD;
}
return (int) rangeSum;
}
}