- Practice
- Algorithms
- Search
- Sherlock and Array
- Discussions

# Sherlock and Array

# Sherlock and Array

himadri77 + 0 comments For the test cases which contains a

**single element**, make sure that you print**YES**, otherwise you will fail the Test #6. I think it is a huge mistake of the author, because it is not specified at all!For example for input:

`1`

`1`

`1`

Make sure your answer is:

`YES`

Apprentice_ + 0 comments The way to do it in O(n) time is to take advantage of the fact that the right and left sides will always contain all the elements except for the current one. So when looping left to right, if one sets the right to contain the sum of all the elements in the array save the first (depends on your implementation), and the left to start off as zero, they can go through the entire array adding the previous element to the left and subtracting the current element from the right then comparing. It becomes a matter of a very simple loop that contains two add/subtract operations and the comparison, and it will only loop over the array one time.

shubhamgoyal1101 + 0 comments python 3 code, easy to understand :) . It takes linear time

def solve(a): # Complete this function summy=sum(a) l=len(a) lefty=0 for i in range(len(a)): current=a[i] summy-=current if lefty==summy: return 'YES' lefty+=current return 'NO'

Kanahaiya + 0 comments Hello friends,

Sherlock and Array hackerrank problem can be solved easily by deriving a linear equation. The complexity of Sherlock and Array hackerrank solution is O(n)

If interested to know more about the generic algorithm in details-

click here for the

**video explanation of generic algorithm**with complexity analysis.or you can click on the image too to follow youtube tutorial.

**Here is the working solution:-****source code :**static String balancedSums(List<Integer> arr) { int x = 0; int sum = 0; for (int a : arr) { sum += a; } for (int y : arr) { if (2 * x == sum - y) { return "YES"; } x = x + y; } return "NO"; }

Would really appreciate your feedback like, dislike , comment etc. on my video.

# Do not forget to upvote, if you find it useful.

emivulpe + 0 comments Python solution. Comments would be appreciated

# Enter your code here. Read input from STDIN. Print output to STDOUT def exists_element(size, arr): sum_left = 0 sum_right = sum(arr) previous = 0 for idx in range(0, size): current = arr[idx] sum_left += previous sum_right -= current if sum_left == sum_right: return "YES" previous = current return "NO" test_cases = int(raw_input()) for i in range(0, test_cases): size = int(raw_input()) arr = map(int, raw_input().split()) print exists_element(size, arr)

Sort 713 Discussions, By:

Please Login in order to post a comment