Sherlock and Array HackerRank Solution

In this Sherlock and Array HackerRank solution, Watson gives Sherlock an array of integers. His challenge is to find an element of the array such that the sum of all elements to the left is equal to the sum of all elements to the right.

Example

 is between two subarrays that sum to .

The answer is  since left and right sum to .

You will be given arrays of integers and must determine whether there is an element that meets the criterion. If there is, return YES. Otherwise, return NO.

Function Description

Complete the balancedSums function in the editor below.

balancedSums has the following parameter(s):

  • int arr[n]: an array of integers

Returns

  • string: either YES or NO

Input Format

The first line contains , the number of test cases.

The next  pairs of lines each represent a test case.
– The first line contains , the number of elements in the array .
– The second line contains  space-separated integers  where .

Constraints




Sample Input

2
3
1 2 3
4
1 2 3 3

Sample Output

NO
YES

Explanation

For the first test case, no such index exists.
For the second test case, , therefore index  satisfies the given conditions.

Sherlock and Array HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in C#

 public static string balancedSums(List<int> arr)
    {
        {
        int length = arr.Count();
        int left = 0;
        int right = 0;
        int last = 0;
        for (int i=0;i<length;i++) 
        {
            left += arr[i];
        }
        for (int i=length-1;i>-1;i--) 
        {
            left-=arr[i];
            right+=last;
            if (left==right) 
            {
                return "YES";
            }
            last = arr[i];
        }
        return "NO";
    }
    }

Problem Solution in Java

public static String balancedSums(List<Integer> arr) {
       
        if (arr.size() == 1 ){ return "YES"; }
       
        int left = 0, sumLeft = arr.get(left), right = arr.size() - 1 , sumRight = arr.get(right);
       
        while ( left < right ){


            if (sumLeft < sumRight ){
               
                sumLeft += arr.get(++left);
               
            } else if (sumLeft > sumRight ){
               
                sumRight += arr.get(--right);
               
            } else if (sumLeft == sumRight) {
               
                sumLeft += arr.get(++left);
                sumRight += arr.get(--right);
            }
           
        }
       
        return ( sumLeft == sumRight ) ? "YES" : "NO";
    }
}

Problem Solution in JavaScript

function balancedSums(arr) {
    let sumLeft = 0;
    let sumRigth = arr.reduce((acc, cur) => acc + cur);
    for(let i = 0; i < arr.length; i++) {
     sumLeft += arr[i - 1] || 0;
     sumRigth -= arr[i];
     if(sumRigth === sumLeft) return "YES";
    }
    return "NO"
}

Problem Solution in Python

def balancedSums(arr):
    # Write your code here
    a=sum(arr)
    add=0
    for x in arr:
        if a-x==add:
            return "YES"
        elif add>a:
            return "NO"
        add+=x
        a-=x
    return "NO"
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment