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-09-08 15:06

bump best I can program is an O(n^2)

make array

calculate product  from initial condition until it hits a 0

if that product is positive, check if it is bigger than max, set it true

if negative
set total product to temp1 and temp2

divide temp1 by the start until it is positive

divide temp2 by the end backwards until it is positive

compare Max(temp1 ,temp2 ) to max, set if true

continue until end of loop

this runs in O(n^2) time from experimental data, though I thought it would have been O(n)

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