Sherlock and Array

  • + 0 comments

    C#

    public static string balancedSums(List<int> arr)
    {
        var len = arr.Count;
        var total = arr.Sum(x => (long)x);
        long leftSum = 0;
        long rightSum = total;
    
        for (int i = 0; i < len; ++i)
        {
            rightSum = rightSum - arr[i];
            var leftIndex = (i == 0) ? 0 : i - 1;
    
            if (i > 0)
            {
                leftSum = leftSum + arr[leftIndex];
            }
    
            if (leftSum == rightSum)
            {
                return "YES";
            }
        }
    
        return "NO";
    }