(Not logged on) | Register | Log On

You can subscribe to this discussion group using an RSS feed reader. techInterview Discussion

Answers to technical interview questions. A part of TechInterview.org

A part of techInterview.org: answers to technical interview questions.

Your host: Michael Pryor

Minimum Positive Subsequence

Hi all, I came across this problem in a book and couldn't solve it. I know the obvious answer that takes O(n^2). Is there any other solution that can solve the problem in O(n)?

Basically we have a sequence of real numbers (positive and negative), we have to find a minimum positive sub sequence.

for example:

1 3 -5 6 Minimum positive subsequence is: -5, 6

1 5 8 -7 3 6 Minimum positive subsequence is 8, -7

Thanks in advance.
Amruth Send private email
Tuesday, June 08, 2010
 
 
n log n is reasonably straightforward. I suspect this is optimal for algorithms that can be translated into an algebraic decision tree.
d Send private email
Tuesday, June 08, 2010
 
 
Please be more specific.
When you say subsequence do you mean not-necessarily-contiguous, or must it be contiguous?
What is a positive subsequence?  One that sums to a positive number?
What is minimal?  Minimum length? Minimum sum? One that cannot be contracted?
A.F.
Wednesday, June 09, 2010
 
 
I mean contiguous subsequence that sums to a least possible positive value. The length of the subsequence can be as long as the size of the given sequence it self, but the sum of the subsequence should be minimum positive value.

hope this pseudo code helps in understanding the problem...


minSum = 0;
for(i=0;i<n;i++)
{
  subSum = 0;
  for(j=0;j<i;j++)
  {
    subSum+=a[j];
  }
  if(subSum>0 && minSum>subSum)
    {
      minSum = subSum;
    }
}
return minSum;
Amruth Send private email
Wednesday, June 09, 2010
 
 
@d.

Yes, Omega(nlogn) is correct. Element distinctness raises its head again. I will leave the reduction to you.
Anonical
Wednesday, June 09, 2010
 
 
Why isn't 1 a minimum positive subsequence for each example?

Sincerely,

Gene Wirchenko
Gene Wirchenko Send private email
Wednesday, June 09, 2010
 
 
Hello,
pardon my ignorance,
What is the O(nlogn) approach to solve this problem..
thnx
puneet Send private email
Thursday, June 10, 2010
 
 
@Gene Wirchenko

Yeah, I am sure 1 is minimum positive sub sequence in both the examples I posted as are -5,6 and 8,-7 .  I am sorry to say that they are not solid examples, but I tried to come up with some example to give the idea.....


Some seem to suggest that O(nlogn) is a optimal solution, but please can some one explain in more detail. May be some hints will be helpful. Pseudocode would be really helpful.

Note: This is not any of my homework problems or any thing like that, I am trying to solve this problem out of my interest and sooner or later I will solve this problem.
Amruth Send private email
Thursday, June 10, 2010
 
 
The key to an O(n log n) algorithm is that given a sequence

x(1) x(2) x(3) ... x(n)

we compute partial sums

y(0) = 0
y(1) = y(0) + x(1)
y(2) = y(1) + x(2) = x(1) + x(2)
y(3) = y2 + x3 = x(1) + x(2) + x(3)
...

so that x(i) + x(i+1) + x(i+2) + ... + x(j) = y(j) - y(i-1). We now are trying to find y(i-1) and y(j) such that i <= j and y(j) - y(i-1) > 0 is minimized, which can be accomplished e.g. by inserting each y value into a balanced binary search tree and determining its predecessor among previously inserted values. I don't know how to reduce the storage requirement from Theta(n) without increasing the running time.

The reduction from element distinctness is in essence to apply the reverse transformation (there are some technical but straightforward details that I don't care to sort out at the moment).
d Send private email
Thursday, June 10, 2010
 
 
(If we make some assumptions about the representation of the input numbers, then e.g. van Emde Boas trees can be used to answer the predecessor queries in o(log n) time.)
d Send private email
Thursday, June 10, 2010
 
 
@d

thanks a lot :)
Amruth Send private email
Thursday, June 10, 2010
 
 
http://www.carsinsurancecompanies.com/ car insurance 075 http://www.free-home-insurance-quotes.net/ cheap homeowners insurance 8-PP http://www.homeinsurance4u.net/ home insurance =-OO http://www.getaffordablehealthinsurance.net/ new york health insurance 117077

Friday, July 02, 2010
 
 
http://www.healthinsurancemate.com/ cheap health insurance 8-)) http://www.slotsguidance.com/ slots 001954 http://www.carsinsurancecompanies.com/ state auto insurance :DD http://www.getcheaphealthinsurance.net/ cheap health insurance pfc

Monday, July 05, 2010
 
 
http://tramadol911.com buy tramadol szl http://viagracomparisons.com/ buy viagra cheap =D http://weightlosspillsonline.org/ buy acomplia bxuush

Wednesday, July 14, 2010
 
 
http://tramadol911.com/ besked buy hjemmeside navn tramadol cccmpt http://viagracomparisons.com/ buy viagra >:-PPP http://weightlosspillsonline.org/ acomplia :-))

Wednesday, July 21, 2010
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz