-2

We want to find largest length subarray having sum greater than k.

One of the solution that works is binary searching on the length of subarray. Length of subarray can vary from 1 to n. We can binary search in range low=1 to high=n and for every mid=(low+high)/2 we can check for all subarray of length=mid if any subarray has sum greater than k in O(n). If any such subarray exist then we can search for higher length subarray i.e. low=mid+1 otherwise we decrease the search length i.e. high=mid-1.

int maxlen(vector<int> v)
{
    int hi = n, lo = 1, ans = -1, mid, cnt = 0;
    while(lo <= hi) {
        mid = hi+lo>>1;
        if(cnt = count(mid)) {
            ans = mid;
            lo = mid + 1;
        } else {
            hi = mid - 1;
        }
     return ans;
 }

int count(int len) {
    int cnt = 0;
    for(int i = len; i <= n; i++)
        if(prefixsum[i] - prefixsum[i - len] > K)
            cnt++;
    return cnt;
}

What puzzles me is that if we get sum < k for present length subarray then increasing the search length of subarray will ensure that we'll get some subarray having subarray>k. And if we have subarray>k for any length then by decreasing search length we can get more such subarrays having greater than k sum. It could be that we could have decreased search length to get some subarray>k. So, the question boils down to deciding the predicate of binary search i.e. at each step how we'll decide to change the search range?

  • 2
    Wouldn't the entire array be the maximum sub array having a sum greater than `k`? – NathanOliver Aug 19 '19 at 21:37
  • why would it be? – Prabhat Sharma Aug 19 '19 at 21:41
  • @NathanOliver I assume some entries may be negative, so this may not always be the case – shayaan Aug 19 '19 at 21:42
  • This kind of a online quiz site question, or an interview question, is looking for a specific algorithm that can easily compute this, in one pass. The shown code isn't it, sorry. The correct solution involves one outer `for` loop, one inner `while` loop, and exactly three variables, in one function. The shown code does this completely backwards, and using an incorrect approach (maybe an outer while loop, instead of a for loop, that's also acceptable). – Sam Varshavchik Aug 19 '19 at 21:42
  • Yes, array contains negative integers also – Prabhat Sharma Aug 19 '19 at 21:43
  • @Shayn That would do it. – NathanOliver Aug 19 '19 at 21:43
  • @SamVarshavchik i know for sure that the above solution works for K=0. But, could you explain why it seems incorrect? Binary searching on required answer when we can verify the answer in O(n) is completely fine. – Prabhat Sharma Aug 19 '19 at 21:45
  • I'm pretty sure that the correct approach involves two simple O(n), linear scans of the array, from the beginning to the end, (still O(n)) instead of some kind of a binary search. Free clue: in the array `a`, if you know that the sum of elements `a[i]` through `a[j]` is `S`, then the sum of `a[i]` through `a[j+1]` is `S+a[j+1]`, and the sum of elements `a[i+1]` thjrough `a[j]` is `S-a[i]`. – Sam Varshavchik Aug 19 '19 at 21:54
  • I don't know how it can be done in O(n). O(n) approaches in such questions are usually 2 pointer approach but you cannot decide which pointer should be forwarded. If the Qn was sum < K then definitely you could have done it in O(n) – Prabhat Sharma Aug 19 '19 at 22:01
  • *"...[I]f we get sum < k for present length subarray then increasing the search length of subarray will ensure that we'll get some subarray having subarray>k."* I've read this several times and I still can't make any sense of it. You seem to be working from some false assumption. Anyway, you must decide whether you want improve your implementation of your algorithm, or find a better algorithm. – Beta Aug 19 '19 at 22:26
  • `int maxlen(vector v)` -- This function is supposed to return an `int`, but does not return anything. This leads to undefined behavior. – PaulMcKenzie Aug 20 '19 at 00:07
  • @Beta I want to know the correctness of my algorithm – Prabhat Sharma Aug 20 '19 at 17:37
  • @PaulMcKenzie I've edited it. Anyways, it was obvious to return ans – Prabhat Sharma Aug 20 '19 at 17:38
  • Your algorithm seems correct, up to *"What puzzles me is...".* I can't understand the logic that comes after that. – Beta Aug 21 '19 at 00:47
  • @Beta Basically, I want to know the proof of correctness. Binary search requires monotonicity in the underlying search space which seems not the case here – Prabhat Sharma Aug 21 '19 at 09:09
  • I see now that I misread the "otherwise" part. The algorithm is incorrect. I will post an Answer. – Beta Aug 21 '19 at 20:13

1 Answers1

0

The algorithm is incorrect (apart from bugs in the implementation).

Consider the array [6 -7 3 0 0 0 3] with k=5.

low=1 high=7 mid=4  no subarray > k
low=1 high=3 mid=2  no subarray > k
low=1 high=1 mid=1  subarray [6] has sum > k
result: [6] with length 1

But the true answer is [3 0 0 0 3] with length 5.

Beta
  • 96,650
  • 16
  • 149
  • 150