MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

RangeSumOfSortedSubarraySums.java:1–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 // [element, index]
3 int mod = (int)1e9+7;
4 public int rangeSum(int[] nums, int n, int left, int right) {
5 PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){
6 public int compare(int a[], int b[]){
7 return a[0] - b[0];
8 }
9 });
10 for(int i=0;i<n;i++){
11 pq.offer(new int[]{nums[i],i});
12 }
13 int sum=0;
14 for(int index=0;index<right;index++){
15 int cur[] = pq.poll();
16 if(index >= left-1){
17 sum = (sum + cur[0])%mod;
18 }
19 // [3,2]
20 if(cur[1]+1<n){
21 cur[1]++;
22 cur[0] = cur[0] + nums[cur[1]];
23 pq.offer(cur);
24 }
25 }
26 return sum;
27 }
28}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected