Sherlock and Array

  • + 14 comments

    Here is My solutin using Java Language. Its time complexity is O(n).

    Explanation(using math logic) - let's denote a middle integer with y and both
    right and left side as x because there sum is same. Now the equation will look like ->
    1) x + y + x = sum of array
    2) 2x + y = sum
    3) 2x = sum - y

    static String balancedSums(List<Integer> arr) {
            int sum = 0;
            int left = 0;
            for(int su : arr){
                sum += su;
            }
            for(int fu : arr){
                if(2*left == sum - fu){
                    return "YES";
                }else{
                    left += fu;
                }
            }
            return "NO";
        }