# HackerRank Sherlock and Array problem solution

In this HackerRank Sherlock and Array problem solution Given an array of integers and we need 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.

## Problem solution in Python.

```import sys
from functools import reduce

def main(argv = None):
if argv is None:
argv = sys.argv

T = int(input())

for t in range(0, T):
N = int(input())
nums = list(map(int, input().split(" ")))

found = 0

pivot = 0
left = 0
right = reduce(lambda x, y: x+y, nums[pivot+1:N+1], 0)
found = (left == right)

left = left + nums[pivot]
right = right - nums[pivot+1]
pivot = pivot + 1
found = (left == right)
if found:
break

if found:
print("YES")
else:
print("NO")

# Invoking the program entry point
if __name__ == "__main__":
sys.exit(main())```

## Problem solution in Java.

```import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Solution
{
public static void main(String[] args)
{
final Scanner in = new Scanner(System.in);
final int numberOfTests = in.nextInt();

final List<Boolean> containWatsonSum = new ArrayList<>(numberOfTests);

for (int i = 0; i < numberOfTests; i++)
{
final int numberOfArrayElements = in.nextInt();

containsWatsonSum(
IntStream
.generate(in::nextInt)
.limit(numberOfArrayElements)
.toArray()));
}

containWatsonSum
.stream()
.map(contains -> contains ? "YES" : "NO")
.forEach(System.out::println);
}

private static boolean containsWatsonSum(int[] array)
{
int leftSum = 0;
int rightSum = IntStream.of(array).skip(1).sum();

for (int i = 0; i < array.length - 1; i++)
{
if (leftSum == rightSum)
{
return true;
}
else
{
leftSum += array[i];
rightSum -= array[i + 1];
}
}

return leftSum == 0;

}
}
```

{"mode":"full","isActive":false}

## Problem solution in C++.

```#include <iostream>
using namespace std;

bool processSherlock(int inp[],int &n)
{
int leftSum[n],rightSum[n];
leftSum[0]=0;
rightSum[n-1]=0;

for(auto i=1;i < n;i++)
{
leftSum[i]=inp[i-1]+leftSum[i-1];
}

for(auto i=n-2; i >=0;i--)
{
rightSum[i]=rightSum[i+1]+inp[i+1];
}

for(auto i=0; i < n;i++)
{
if(leftSum[i]==rightSum[i])
return true;
}

return false;

}

int main(int argc, const char * argv[])
{
int t;
cin >> t;
while(t-- > 0)
{
int n;
cin >> n;
int inp[n];
for(auto i=0; i < n;i++)
cin >> inp[i];
if(processSherlock(inp,n))
cout << "YES" << endl;
else
cout << "NO" << endl;

}
return 0;
}

```

{"mode":"full","isActive":false}

## Problem solution in C.

```#include <stdio.h>
#include <stdlib.h>

int main() {
int i,j;
int sum, left, right;
unsigned int T, N;
unsigned int A[100000];
scanf("%d", &T);
for (i=0; i<T; i++) {
scanf("%d", &N);
for (j=0; j<N; j++){
scanf("%d", &A[j]);
}
if (N==1) {
printf("YES\n");
continue;
}
else {
left = 0;
right = N - 1;
sum = 0;
while (left != right) {
if (sum < 0) {
sum += A[right];
right--;
}
else if (sum > 0) {
sum -= A[left];
left++;
}
else if (sum == 0) {
if (A[left] > A[right]) {
sum = A[right];
right--;
}
else {
sum = 0 - A[left];
left++;
}
}
}
if (sum == 0) printf("YES\n");
else printf("NO\n");
}
}
}```

{"mode":"full","isActive":false}