Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.45 KB | None | 0 0
  1. function solution($A) {
  2.   $count = count($A);
  3.   $left = 0;
  4.   $right = 0;
  5.  
  6.   if (!$count) {
  7.     return -1;
  8.   }
  9.  
  10.   if ($count == 1) {
  11.     return 0;
  12.   }
  13.  
  14.   for ($n = 1; $n < $count; $n++) {
  15.     $right += $A[$n];
  16.   }
  17.  
  18.   if ($left == $right) {
  19.       return 0;
  20.   }
  21.  
  22.   for ($n = 1; $n < $count; $n++) {
  23.       $left += $A[$n - 1];
  24.       $right -= $A[$n];
  25.  
  26.       if ($left == $right) {
  27.           return $n;
  28.       }
  29.   }
  30.  
  31.   return -1;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement