How would I answer this using PHP?

You decided that you would give a shot at riding a unicycle while balancing weights on both your hands. To make sure that you don't fall, you want the weights in your hands to balance out as much as possible (i.e. the difference between the sum of the weights in either of your hands must be as small as possible). Given the weights you have, determine the smallest difference in weight between your left and right hands.

Sample Input: 1 10 6

Sample Output: 3

Also, you don't have to use all the weights, but you can't use 0 weights. I'm thinking that a recursive function would be the best way to solve this.

By the way, this isn't my homework or anything, it's a question on a past Dwite contest.

Answers


My PHP is a little rusty, but something like this should work:

$left = 0; $right = 0;
for_each($input as $value) {
  if ($left < $right) {
    $left += $value;
  } else {
    $right += $value;
  }
}
$answer = abs( $left - $right );

Need Your Help

How do I stop or remove a jQuery function from a HTML element

javascript jquery html function

I am attaching a function like so, which loads with the page or when called again:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.