2
$\begingroup$

I have a long array of size $n>10^6$, call it $X$. I would like to find all ranges $[a, b)$ satisfying the following conditions

  1. $\sum_{i=a}^{b-1}X[i] \leq 0$,
  2. $b-a \geq K$,
  3. $\sum_{i=a-1}^{b-1} X[i] > 0$,
  4. $\sum_{i=a}^{b} X[i] > 0$.

I also would like to note that $X[i]$s can only take $m$ distinct values, typically $m<10$. A quadratic time algorithm is trivial. I'm wondering if I can do any better than that.

asked Nov 14 at 18:08
New contributor
cebir latis is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
7
  • $\begingroup$ Can you use prefix sums to advantage? $\endgroup$ Commented Nov 15 at 11:17
  • 1
    $\begingroup$ "[time] any better than [quadratic]" you may have to do an output sensitive analysis ($t = f(n) + c_2(|output|)$): there may be $O(n^2)$ such ranges/pairs. $\endgroup$ Commented Nov 15 at 11:25
  • $\begingroup$ Oh, got it. I think I'll have to come up with a more constrained version of 3 and 4. Thanks! $\endgroup$ Commented 2 days ago
  • $\begingroup$ Is $K$ small compared to $n$ or large? Would you be satisfied with a sub-quadratic algorithm that finds a single such interval? Do you already know of such an algorithm? $\endgroup$ Commented 2 days ago
  • 1
    $\begingroup$ I came up with an $O(n \log n)$ for the relaxed version I mentioned in my above comment. I'll add as an answer once I am confident. $\endgroup$ Commented 2 days ago

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.