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

Pages: 1-4041-

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-28 17:32

I can't tell if he's joking or not.

Name: Anonymous 2010-08-28 18:01

he's not

Name: Anonymous 2010-08-28 18:55

Let X = {x1, x3, ..., xn} be a sequence of arbitrary real numbers. Give a linear time algorithm to find the subsequence of consecutive elements xi, xi+1 , ..., xj whose product is maximum over all consecutive subsequences. The product of the empty subsequence is defined as 1. Observe that X can contain reals less than 1, including negative
numbers.

Name: Anonymous 2010-08-28 19:50

Let's not listen.

Name: Anonymous 2010-08-28 23:09

bump for help

Name: Anonymous 2010-08-28 23:56


maxProduct(a[]){
lef tP osP rod = 1 // product of the right positive segment
rightP osP rod = 1 //product of the left positive segment
lef tN egative = U N DEF IN ED //value of the left-most negative
rightN egative = U N DEF IN ED //value of the right-most negative
totalP rod = 1 //product of entire array
negCount = 0 //keeps track of the number of negatives encountered

for(i = 0; i < a.size; i + +;){
totalProd ∗ = a[i]
if(a[i] > 0 and lef tN egative = U N DEF IN ED)
leftPosProd ∗ = a[i]
else if(a[i] > 0 and rightN egative = U N DEF IN ED)
rightPosProd ∗ = a[i]
else if(a[i] < 0 and lef tN egative = U N DEF IN ED){
negCount + +
rightN egative = a[i]
rightP osP rod = 1
}
else //a[i] is the first negative encountered
negCount + +
lef tN egative = rightN egative = a[i]
}
}
if((negCount > 0 and a.size = 1) or negCount %2 = 0)
return totalP rod
else
return totalP rod/minimum(leftPosProd*leftNegative,rightPosProd*rightNegative)

}


but it doesn't work properly

Name: Anonymous 2010-08-29 0:39

Because multiplication is associative, bitches.


maxProduct :: RealFrac a => [a] -> a
maxProduct = f 1 1 1
  where
    f maxp _ _ [] = maxp
    f maxp mina a (x:xs) =
      let a' = a * x
          p = a' / mina
      in f (max maxp p) (min mina a') a' xs

Name: Anonymous 2010-08-29 0:47

>>8
Uh, note that you have to split on the zeroes first.  TTFN.

Name: Anonymous 2010-08-29 1:51

>>8
what language is that?

Name: Anonymous 2010-08-29 1:55

>>10
VB++

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?

Name: Anonymous 2010-08-29 2:20

>>12
Not correct.  Here is a case in which your algorithm fails:

0.5, -2, -2, -2, 0.25

Your algorithm selects (0.5, -2, -2) giving 2.  The correct choice is (-2, -2) giving 4.

Name: Anonymous 2010-08-29 2:23

>>13
all of the numbers are integers

Name: Anonymous 2010-08-29 3:03

>>14
Fuck, if you can't even tell us the problem you're trying to solve, then just die in a fire.

Name: Anonymous 2010-08-29 3:11

>>15
sorry

numbers are integers between -128 and 127 including 0


0 -5 -8 0 2 = 40
6 -4 -3 = 72

Name: Anonymous 2010-08-29 4:05

>>16
ARE THERE OTHER HIDDEN FUCKING DETAILS YOU FORGOT OR WILL THAT BE JUST ABOUT IT?

Name: Anonymous 2010-08-29 6:04

This may work:

-Split array into sub-arrays between zeros. (O(n))
-Check how many negative numbers each sub-array has. (O(n))
-If even, then multiply out the whole sub-array. (O(n))
-If odd, then you would have two products: one using everything after the first negative number and one using everything before the last negative number. Take the larger of the two. (O(n))
-Find the largest value out of all the sub-arrays. (O(n))

Name: Anonymous 2010-08-29 10:26

>>17
there can be up to 10,000 elements in the array, so I should probably use bigInteger

>>18
-If odd, then you would have two products: one using everything after the first negative number and one using everything before the last negative number. Take the larger of the two. (O(n))

that makes more sense than doing all the calculations and then dividing out. But I would have to have a location of the last negative before I start doing the product?

Name: Anonymous 2010-08-29 10:38

so I can make a function
product(ar[],start,end)

findFirstNeg(ar[]){...}

findLastNeg(ar[]){...}

then
temp = product(ar,0,findLastNeg)
temp2 = product(ar,findFirstNeg,ar.length-1)
return(temp>temp2?temp1:temp2)

Name: Anonymous 2010-08-29 10:59

>>19
But I would have to have a location of the last negative before I start doing the product?
f = reverse . tail . (dropWhile (>= 0)) . reverse

Name: Anonymous 2010-08-29 11:00

>>20
This post is an abomination.

Name: Anonymous 2010-08-29 13:36

>>22
how so?

also stop  saging

Name: Anonymous 2010-08-29 13:45

also stop  saging
[i]back to /b/, please.[/b]

Name: Anonymous 2010-08-29 13:46

>>24
Maybe if you didn't drop out of CS you could write BBCode correctly.

Name: Anonymous 2010-08-29 13:53

>>23
How about no [code] tags for starters.
How about you understand ``saging''.

Name: Anonymous 2010-08-29 13:55

>>26
I was just spitballing syntax

Name: Anonymous 2010-08-29 14:03

>>27
This post does not address any of the issues pointed out in >>26. As Xarn would put it, you're an idiot.

Name: Anonymous 2010-08-29 14:05

>>21
what about



i = arr.length

while(arr[i]>0)
{
arr[i--] = 1;
}


since you can multiply anything by one and get the same result

Name: Anonymous 2010-08-29 17:12

>>29
Nice ArrayOutOfBoundsException there, Java user.

Name: Anonymous 2010-08-29 18:15

Adapt the maximum consecutive subarray sum algorithm (which is linear time) to your solution.

http://en.wikipedia.org/wiki/Maximum_subarray_problem

Name: Anonymous 2010-08-30 16:06

>>30
how so? there has to be at least one negative number

Name: Anonymous 2010-08-30 16:08

>>32
See if you can guess what happens when you try to access arr[arr.length], as you do on the first pass of that loop.

Name: Anonymous 2010-08-30 17:29

>>33
If this weren't Java, fun stuff would happen.

Name: Anonymous 2010-08-30 18:07

>>33
arr[arr.length-1] because they start at 0, gotcha

Name: Anonymous 2010-08-30 18:17

>>35
Your posts are still disturbingly obnoxious.

Name: Anonymous 2010-08-30 18:49

>>36
ur face

Name: Anonymous 2010-08-31 7:23

Best thread in weeks

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)

Name: Anonymous 2010-09-08 15:23

use jewlib

Name: Anonymous 2010-09-08 15:58

Jewish liberation ?

Name: Anonymous 2010-09-08 19:27

>>39
You might just be seeing the breakdown of your ``multiplication and division are O(1) time'' assumption. The divisions can be replaced by keeping both negative and positive running totals.

Name: Anonymous 2010-09-08 21:29

>>42
The divisions can be replaced by keeping both negative and positive running totals.

what do you mean by this?

Name: Anonymous 2010-09-09 3:46

>>43
Maybe he means that you should learn to program.

Name: Anonymous 2010-12-17 1:31

Xarn is a bad boyfriend

Name: Anonymous 2010-12-17 1:39

This post brought to you by the Gay Nigger Association of America

Name: Anonymous 2013-01-19 23:38

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

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