**Sum of Sublist Range Sum - Google Top Interview Questions**

### Problem Statement :

You are given a list of non-negative integers nums and integers i and j. Let A be a sorted list of the sum of every sublist of nums. Return the sum of A from [i, j], inclusive. Constraints 1 ≤ n ≤ 100,000 where n is the length of nums Example 1 Input nums = [1, 2, 3, 4] i = 2 j = 3 Output 6 Explanation A = [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] here and sum([3, 3]) = 6.

### Solution :

` ````
Solution in C++ :
typedef long long ll;
int solve(vector<int>& nums, int i, int j) {
int n = nums.size();
vector<ll> pre(n + 1);
for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + nums[i - 1];
vector<ll> prepre(n + 1);
for (int i = 1; i <= n; ++i) prepre[i] = prepre[i - 1] + pre[i];
auto count = [&](ll sum) {
ll ans = 0;
for (int i = 1; i <= n; ++i)
ans += upper_bound(pre.begin() + i, pre.end(), sum + pre[i - 1]) - (pre.begin() + i);
return ans;
};
auto calc = [&](ll p) {
ll l = 0, r = pre[n];
while (l <= r) {
ll mid = (l + r) >> 1;
if (count(mid) < p) {
l = mid + 1;
} else {
r = mid - 1;
}
}
ll smaller = count(l - 1);
ll equal = p - smaller;
ll sum = 0;
for (int i = 1; i <= n; ++i) {
int pos = upper_bound(pre.begin() + i, pre.end(), pre[i - 1] + l - 1) - pre.begin();
if (pos > i) sum += prepre[pos - 1] - prepre[i - 1] - pre[i - 1] * (pos - i);
}
return sum + equal * l;
};
return calc(j + 1) - calc(i);
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public int solve(int[] nums, int ii, int jj) {
int n = nums.length, res = 0, s = 0;
for (int a : nums) s += a;
long[] sum = new long[n], sum2 = new long[n];
sum[0] = sum2[0] = nums[0];
for (int i = 1; i < n; ++i) {
sum[i] = sum[i - 1] + nums[i];
sum2[i] = sum2[i - 1] + sum[i];
}
int low = findBound(nums, ii, s), high = findBound(nums, jj, s);
int countHigh = count(high, nums), countLow = count(low, nums);
res = low * Math.min(countLow - ii, jj - ii + 1);
++low;
if (low > high)
return res;
for (int a = -1, b = -1, k = 0, sa = 0, sb = 0; k < n; ++k) {
long pre = k > 0 ? sum[k - 1] : 0;
while (b + 1 < n && sum[b + 1] - pre <= high) ++b;
while (a < k || (a <= b && sum[a] - pre < low)) ++a;
if (a <= b) {
long t = sum2[b] - (a > 0 ? sum2[a - 1] : 0);
if (k > 0)
t -= sum[k - 1] * (b - a + 1);
res += (int) t;
}
}
if (countHigh > jj + 1)
res -= high * (countHigh - jj - 1);
return res;
}
private int findBound(int[] ns, int idx, int max) {
int l = 0, r = max, mid;
while (l <= r) {
mid = (l + r) >> 1;
int t = count(mid, ns);
if (t >= idx + 1)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
private int count(int v, int[] ns) {
int s = 0, res = 0, p = 0;
for (int i = 0; i < ns.length; ++i) {
s += ns[i];
while (s > v && p <= i) {
s -= ns[p++];
}
res += i - p + 1;
}
return res;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, nums, i, j):
n = len(nums)
a = list(itertools.accumulate([0] + nums))
b = list(itertools.accumulate([0] + a))
def cnt(ub):
c, u = 0, 0
for v in range(1, n + 1):
while a[v] - a[u] > ub:
u += 1
c += v - u
return c
def go(m):
lo, hi = 0, a[n]
while lo < hi:
mid = (lo + hi) // 2
if cnt(mid) < m:
lo = mid + 1
else:
hi = mid
s, u = 0, 0
for v in range(1, n + 1):
while a[v] - a[u] > lo:
u += 1
s += a[v] * (v - u) - (b[v] - b[u])
return s - (cnt(lo) - m) * lo
return go(j + 1) - go(i)
```

## View More Similar Problems

## Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →## Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →## Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →## Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →