**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

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →