Skip to content

Create 1508. Range Sum of Sorted Subarray Sums #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 1508. Range Sum of Sorted Subarray Sums
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
const int mod = 1e9+7;
public:
int rangeSum(vector<int>& nums, int n, int l, int r) {
vector<int> arr;

// Generate all subarray sums
for(int i = 0; i < n; i++) {
int sum = 0;
for(int j = i; j < n; j++) {
sum = (sum + nums[j]) % mod;
arr.push_back(sum);
}
}

// Sort the subarray sums
sort(arr.begin(), arr.end());

// Compute the range sum
int sum = 0;
for(int i = l - 1; i < r; i++) {
sum = (sum + arr[i]) % mod;
}

return sum;
}
};
Loading