1

Suppose we have the array [1,2,3,4]
We want the amount of times where the sum of a contiguous subarray is for example in between l = 5 and u = 7.
We can say this is the case for 1+2+3, 3+4 So the answer would be 2.

It is easily solved in O(n²) time, by using two for loops. Now how can this be solved in O(n log² n) time using a binary search tree? Negative values are included. Single values are also counted as a subarray.

My idea would be to loop over the left half of the array

i=0=> 1+5=6
      1+7=8
i=1=> 2+5=7
      2+7=9

Then use those values to perform a binary search in the right part (we sort the right part of the prefix sum) of the prefix sum array which would be [1,3,6,10].

We try to find any of the values in the right part [6,10].

You can see the value 6 occurs in there. So the left bound is found. However the right bound is not found.

static int CountOverSplit(int[] prefixSum, int s, int m, int e, int lb, int ub, int[] input)
{
    for (int i = s; i <= m; i++)
    {
        int lower = input[i] + lb;
        int upper = input[i] + ub;
        indexMinLeft = BinarySearchRecursive(prefixSum, lower, m, e - 1);
        indexMaxRight = BinarySearchRecursive(prefixSum, upper, m, e - 1);
    }
    return indexMinLeft - indexMaxRight;
}


public static int BinarySearchRecursive(int [] inputArray, int key, int min, int max)  
{  
    if (min > max)  
    {  
        return -1;  
    }  
    else  
    {  
        int mid = (min+max)/2;  
        if (key == inputArray [mid])  
        {  
            return ++mid;  
        }  
        else if (key < inputArray [mid])  
        {  
            return BinarySearchRecursive(inputArray, key, min, mid - 1);  
        }  
        else  
        {  
            return BinarySearchRecursive(inputArray, key, mid + 1, max);  
        }  
    }  
} 

I have been breaking my head over this for so long now, I really can't seem to figure it out.

Not sure if this belongs to stackoverflow or another stack, I'm sorry if it has been posted wrong. Thanks in advance. The question seems to be similar to this one, for reference.

kcsquared
  • 5,244
  • 1
  • 11
  • 36
Joost Luijben
  • 174
  • 3
  • 13
  • It's very close to [the question that you linked](https://stackoverflow.com/questions/24543672/google-interview-find-all-contiguous-subsequence-in-a-given-array-of-integers). The only difference I can see is between the *"better than O(n²)"* and the *"in O(n log² n)"*. Your question asks for a more precise O value. It probably qualifies as a duplicate IMHO. – Theodor Zoulias Feb 16 '22 at 16:47
  • Yes but I can't find working code on that post that also works for negative values and uses a binary search tree.... – Joost Luijben Feb 16 '22 at 16:48
  • Is it a requirement to use a binary search tree? Your question indicates that it's is just your attempt of solving this problem. – Theodor Zoulias Feb 16 '22 at 16:51
  • Yeah it does, maybe I should change the question. There you go! – Joost Luijben Feb 16 '22 at 16:51
  • 1
    You should probably emphasize inside the question that solutions non involving a binary search tree are not acceptable. Otherwise people may try to solve this in different ways. – Theodor Zoulias Feb 16 '22 at 16:55
  • Am I right in thinking that any binary search tree algorithm would not only count the number of such occurences but also exactly identify each of them? There are many instances of this problem in which there are n^2 solutions, so to identify each of them would require n^2 steps. – Christopher Hamkins Feb 16 '22 at 17:32
  • btw how do you get superscripts in comments? – Christopher Hamkins Feb 16 '22 at 17:32
  • 1
    @ChristopherHamkins ² is just a [unicode symbol](https://www.compart.com/en/unicode/U+00B2). – Theodor Zoulias Feb 16 '22 at 17:35
  • 1
    Shouldn't the answer for `[1,2,3,4]` and l = 5, r = 7 be 3, since [2, 3] counts too? Anyways this question seems to be very close to the linked one apart from requesting a binary search tree based solution. There are several answers to the linked question using binary search techniques which can be adapted to a BST (but none using one explicitly), so technically this might not be a duplicate of that question. – kcsquared Feb 16 '22 at 20:22
  • 1
    This problem is similar to LeetCode 327: https://leetcode.com/problems/count-of-range-sum/. Check out the discussion section, there are several BST solutions. – Ruslan Gilmutdinov Mar 16 '22 at 13:04

0 Answers0