Sherlock and Array

  • + 0 comments

    Perl solution:

    sub balancedSums {
        my $arr = shift;
        
        my $total_sum = sum(@$arr);
        my $left_sum = 0;
        foreach my $num (@$arr) {
            my $right_sum = $total_sum - $left_sum - $num;
            return "YES" if $left_sum == $right_sum;
            $left_sum += $num;
        }
        return "NO";  
    }