# Kth Smallest sum of continuous subarrays of positive numbers

Given an sorted array of positive numbers our tasks is to find the kth smallest sum of continuous subarray.

Examples:

Input : a[] = {1, 2, 3, 4, 5, 6}

k = 4

Output : 3

Explanation : List of sorted subarray sum: {1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 9, 9, 10, 11, 12, 14, 15, 15, 18, 20, 21}. 4th smallest element is 3.Input : a[] = {1, 2, 3, 4, 5, 6}

k = 13

Output: 10

A **naive approach** is to first generate all the continuous subarray sums which can be done in O(N^2) by precomputing prefix sum. Sort the sum array and given the kth smallest element.

A **better approach (for arrays with small sum)** is to use Binary search. First we will precompute a prefix sum array. Now we apply binary search on the number which are possible candidates for the kth smallest sum which will be in range [0, total sum of the array]. For now lets assume we have a function called calculateRank which will give us the rank of any number in the sorted array of continuous subarray sum. In binary search we will use this calculateRank function to check if the rank of the mid element is less than K, if yes then we reduce the start point to mid+1 else if it is greater than or equal to K, then reduce the end point to mid-1 and also updating the answer variable.

Now lets come back to calculateRank function. Here, also we will use binary search but on our prefix sum array.We will iterate on our array and lets assume we are on ith index, we will count how many subarray can be made with starting element as this ith element whose sum is less than the mid element whose rank we have to calculate. We do for all elements and add the count of each which we the rank of the mid element. To count the number of the subarray starting with ith index we use apply binary on prefix sum.

C++ implementation to make things clearer.

`// CPP program to find k-th smallest sum` `#include "algorithm"` `#include "iostream"` `#include "vector"` `using` `namespace` `std;` ` ` `int` `CalculateRank(vector<` `int` `> prefix, ` `int` `n, ` `int` `x)` `{` ` ` `int` `cnt;` ` ` ` ` `// Initially rank is 0.` ` ` `int` `rank = 0;` ` ` `int` `sumBeforeIthindex = 0;` ` ` `for` `(` `int` `i = 0; i < n; ++i) {` ` ` ` ` `// Calculating the count the subarray with` ` ` `// starting at ith index` ` ` `cnt = upper_bound(prefix.begin(), prefix.end(), ` ` ` `sumBeforeIthindex + x) - prefix.begin();` ` ` ` ` `// Subtracting the subarrays before ith index.` ` ` `cnt -= i;` ` ` ` ` `// Adding the count to rank.` ` ` `rank += cnt;` ` ` `sumBeforeIthindex = prefix[i];` ` ` `}` ` ` `return` `rank;` `}` ` ` `int` `findKthSmallestSum(` `int` `a[], ` `int` `n, ` `int` `k)` `{` ` ` `// PrefixSum array.` ` ` `vector<` `int` `> prefix;` ` ` ` ` `// Total Sum initially 0.` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 0; i < n; ++i) {` ` ` `sum += a[i];` ` ` `prefix.push_back(sum);` ` ` `}` ` ` ` ` `// Binary search on possible` ` ` `// range i.e [0, total sum]` ` ` `int` `ans = 0;` ` ` `int` `start = 0, end = sum;` ` ` `while` `(start <= end) {` ` ` ` ` `int` `mid = (start + end) >> 1;` ` ` ` ` `// Calculating rank of the mid and ` ` ` `// comparing with K` ` ` `if` `(CalculateRank(prefix, n, mid) >= k) {` ` ` ` ` `// If greater or equal store the answer` ` ` `ans = mid;` ` ` `end = mid - 1;` ` ` `}` ` ` `else` `{` ` ` `start = mid + 1;` ` ` `}` ` ` `}` ` ` ` ` `return` `ans;` `}` ` ` `int` `main()` `{` ` ` `int` `a[] = { 1, 2, 3, 4, 5, 6 };` ` ` `int` `k = 13;` ` ` `int` `n = ` `sizeof` `(a)/` `sizeof` `(a[0]);` ` ` `cout << findKthSmallestSum(a, n, k);` ` ` `return` `0;` `}` |

**Output:**

10

**Time Complexity:** O(N Log N Log SUM). Here N is number of elements and SUM is array sum.

CalculateRank function takes O(N log N) time and is called Log SUM times.