Sherlock and Array

  • + 0 comments

    php

    function balancedSums($arr) {
        $n = count($arr);
        $sum_total = array_sum($arr);
        $sum_left = 0;
    
        for($i = 0; $i < $n; $i++) {
            if($i == 0) {
                $sum_left = 0;
            } else {
                $sum_left += $arr[$i - 1];
            }
    
            $sum_right = $sum_total - $sum_left - $arr[$i];
            if($sum_left == $sum_right) {
                return 'YES';
            }
        }
        return 'NO';
    }