Re: array_sum($result)=100

From: Date: Sun, 24 Sep 2006 16:41:12 +0000
Subject: Re: array_sum($result)=100
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-242169@lists.php.net to get a copy of this message
On Sun, 24 Sep 2006 19:06:11 +0300, "Ahmad Al-Twaijiry" <ahmadt@gmail.com> wrote: > Hi everyone > > I have array of numbers and I want to get out of it a list of numbers > that if I sum them it will be 100, here is my list (for example ) : > > $list = array(10,20,10,10,30,50,33,110,381,338,20,11,200,100); > > > I want the result to be : > > $result = array( 10,20,10,10,50); > > as you can see in the array $result , if we array_sum($result) the > result will be 100. > > is they any algorithm to do this ? I think you can try to sort the array in reverse order, and then calculate the sum of numbers in loops (end loop when the sum is larger than target sum). It seems there is no algorithm can do this quickly.

« previous php.general (#242169) next »