Table of Contents

# Sherlock and Array – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

# Solutions of Algorithms Data Structures Hard HackerRank:

## Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts

# 1 Month Preparation Kit Solutions – HackerRank

# C++ Sherlock and Array HackerRank Solution

#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; }

# Java Sherlock and Array HackerRank Solution

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(); containWatsonSum.add( 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; } }

# Python 3 Sherlock and Array HackerRank Solution

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) while (not found) and (pivot < N-1): 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())

# JavaScript Sherlock and Array HackerRank Solution

# C Sherlock and Array HackerRank Solution

#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"); } } }

## Leave a comment below