Sherlock and Array

Sort by

recency

|

54 Discussions

|

  • + 0 comments

    For arr s.t. arr == {1}, the left and right can't add up to zero. Even saying there is a "left" and a "right" is nonsensical. Null + Null != 0.

  • + 0 comments
    def balancedSums(arr):
        # Write your code here
        total_sum = sum(arr)
        right_acc = 0
        for index in range(len(arr)):
            total_sum -= arr[index]
            if (right_acc == total_sum):
                return "YES"
            right_acc += arr[index]
        return "NO"
    
  • + 0 comments

    def balancedSums(arr):

    left = 0
    right = sum(arr)
    for i in range(len(arr)):
        right -= arr[i]
        if left == right:
            return 'YES'
        left += arr[i]
    return 'NO'
    
  • + 0 comments

    TypeScript

    function balancedSums(arr: number[]): string {
      let totalSum = arr.reduce((acc, cur) => acc + cur);
    
      let leftSum = 0;
    
      for (let i = 0; i < arr.length; i++) {
        let rightSum = totalSum - leftSum - arr[i];
    
        if (rightSum === leftSum) return 'YES';
    
        leftSum += arr[i];
      }
    
      return 'NO';
    }
    
  • + 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';
    }