I am trying to find an O(n) solution to this problem:
Given an array, find the subarray with the sum closest to 0.
An O(nlogn) solution is kinda straightforward, but I have read this problem in a textbook in a context suggesting there's a O(n) solution. Any help?
i don't know but looks like it's a variant of kadane algorithm
Not as far as my thought process could go. A variation of Kadane algorithm can be used to find the minimum sum; but could not apply that to a specific sum.
The simplest form of this problem I could find is finding the closest two elements in an array (the prefix sums in this problem).
You can't solve this faster than O(n log n) by a reduction from https://en.wikipedia.org/wiki/Element_distinctness_problem
The element distinctness problem only applies for comparison sorts. This reduces to 2SUM and you can use a hash table to do this, assuming a uniform hash.
Bro you're a little late...)
Nice you dug up this ancient post. Go read about necroposting