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

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

Hello everyone. It would be great if someone gives a solution for this problem https://www.codechef.com/problems/CZ17R2Q2. Thank you.

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

»
7 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

Maintain a stack of length n. For every i-th element, if it is positive, push it into the stack. If it is negative, let's call it x. Check if stack.top() == -x. If yes, pop stack.top() and increase the counter by 2.

Solution complexity — O(n).

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Basically, you have to find the largest subarray whose sum is equal to zero. You can look at my submission here