Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

MCSP

Name: Anonymous 2010-08-28 17:19

How do you get an O(n) running time of a Maximum Consecutive Sub-sequence Product. I have a O(n^2) algorithm now, but I need a O(n) runtime.

ar is the array

k=ar.length;
          

            pro=BigInteger.ONE;
            max=BigInteger.valueOf(ar[0]);
            for(i=0;i<k;i++)
            {
                pro=BigInteger.valueOf(ar[i]);
                if(pro.compareTo(max)>0) {
                    max=pro;
                }
                for(j=i+1;j<k;j++)
                {
                    pro=pro.multiply(BigInteger.valueOf(ar[j]));
                    if(pro.compareTo(max)>0) 
                        max=pro;
                }

Name: Anonymous 2010-08-29 2:02

>>11
I have no idea what that code is doing.

Anyway, what I was thinking of was that I have to split arrays to get rid of the 0s.  And I will have a sub array. And this array can have either an even or odd number of negatives. If it is an even number of negatives, I just calculate the product.  If there are an odd number of negatives I divide the product by the front until I hit the negative number, then I save that as temp1, I go back to the original product and divide by the last number working backwards until I hit the first negative number and save that as temp2, and then I can just return the max of temp1,temp2.

sounds correct?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List