Блог пользователя el3el2

Автор el3el2, история, 8 лет назад, По-английски

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?

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

i don't know but looks like it's a variant of kadane algorithm

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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).