HackerRank Sherlock and Array Problem Solution Yashwant Parihar, May 7, 2023May 7, 2023 In this post, we will solve HackerRank Sherlock and Array Problem 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.Examplearr = [5, 6, 8, 11]8 is between two subarrays that sum to 11.arr = [1]The answer is [1] since left and right sum to 0.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 DescriptionComplete the balancedSums function in the editor below.balancedSums has the following parameter(s):int arr[n]: an array of integersReturnsstring: either YES or NOInput FormatThe first line contains T, the number of test cases.The next T pairs of lines each represent a test case.-The first line contains n, the number of elements in the array arr.-The second line contains n space-separated integers arr[i] where 0 < i < n.Sample Input 02 3 1 2 3 4 1 2 3 3 Sample Output 0NO YESExplanation 0For the first test case, no such index exists.For the second test case, arr[0] + arr[1] = arr[3], therefore index 2 satisfies the givenconditions.Sample Input 13 5 1 1 4 1 1 4 2 0 0 0 4 0 0 2 0 Sample Output 1YES YES YESExplanation 1In the first test case, arr[2] = 4 is between two subarrays summing to 2. In the second case, arr[0] = 2 is between two subarrays summing to 0. In the third case, arr[2] = 2 is between two subarrays summing to 0.HackerRank Sherlock and Array Problem SolutionSherlock and Array C Solution#include <stdio.h> #define MAXN 100005 int main () { unsigned long a[MAXN], t, n, i; unsigned long long sum, leftSum[MAXN] = { 0 }; scanf("%ld", &t); while ( t-- ) { scanf("%lu", &n); sum = 0; for ( i = 0; i < n; i++ ) { scanf("%lu", a+i); leftSum[i] = sum; sum += a[i]; } for ( i = 0; i < n; i++ ) if ( leftSum[i] == sum - a[i] - leftSum[i] ) { printf("YES\n"); break; } if ( i == n ) printf("NO\n"); } return 0; }Sherlock and Array C++ Solution#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int t; cin>>t; for(int i=0;i<t;i++){ int n; cin>>n; vector<int>A; vector<long long> left; long long sum = 0 ; for(int j=0; j<n; j++){ int ai ; cin>>ai; A.push_back(ai); left.push_back(sum); //cerr<<"j= "<<j<<" , left sum="<<sum<<endl; sum += ai; } sum=0; bool found = false; for(int j=n-1; j>=0; j--){ if(sum==left[j]){ found = true; cout<<"YES"<<endl; break; } //cerr<<"j= "<<j<<" , right sum="<<sum<<endl; sum += A[j]; } if(!found) cout<<"NO"<<endl; } /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }Sherlock and Array C Sharp Solutionusing System; class Solution { private static bool Exist( long [] arrNums ) { // initialize cursors int iLeft = 0, iRight = arrNums.Length - 1; long diff = arrNums[ iLeft ] - arrNums[ iRight ]; while ( iLeft < iRight ) { // compare sum difference and move cursors if ( diff < 0 ) { diff += arrNums[ ++iLeft ]; } else if ( diff > 0 ) { diff -= arrNums[ --iRight ]; } else { diff += arrNums[ ++iLeft ] - arrNums[ --iRight ]; } } return ( diff == 0 ); } public static void Main( string [] args ) { // input T int nCases = Convert.ToInt32( Console.ReadLine() ); while ( nCases-- > 0 ) { // input N int nNums = Convert.ToInt32( Console.ReadLine() ); // input numbers var strNums = Console.ReadLine().Split(); var arrNums = new long[ nNums ]; for ( int i = 0; i < nNums; ++i ) { arrNums[ i ] = Convert.ToInt32( strNums[ i ] ); } // output results Console.WriteLine( Exist( arrNums ) ? "YES" : "NO" ); } return; } }Sherlock and Array Java Solutionimport java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'balancedSums' function below. * * The function is expected to return a STRING. * The function accepts INTEGER_ARRAY arr as parameter. */ public static String balancedSums(List<Integer> arr) { // Write your code here int totalsum = 0,leftsum = 0 , rightsum = 0; for(int i = 0; i<arr.size(); i++){ totalsum = totalsum + arr.get(i); } for(int i = 0; i<arr.size(); i++){ rightsum = totalsum - arr.get(i) - leftsum; if(leftsum == rightsum){ return "YES"; } else{ leftsum = leftsum +arr.get(i); } } return "NO"; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int T = Integer.parseInt(bufferedReader.readLine().trim()); IntStream.range(0, T).forEach(TItr -> { try { int n = Integer.parseInt(bufferedReader.readLine().trim()); List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()); String result = Result.balancedSums(arr); bufferedWriter.write(result); bufferedWriter.newLine(); } catch (IOException ex) { throw new RuntimeException(ex); } }); bufferedReader.close(); bufferedWriter.close(); } }Sherlock and Array JavaScript Solutionfunction check(arr) { var sum = arr.reduce(function(el, s){ return s + el; }); var p1 = 0, p2; var answer = 'NO'; for (var i = 1; i < arr.length; i++) { p1 += arr[i-1]; p2 = sum - p1 - arr[i]; if (p1 === p2) { answer = 'YES'; break; } if (p1 > p2) { break; } } if (arr.length === 1) { answer = 'YES'; } console.log(answer); } function processData(input) { var parse_fun = function (s) { return parseInt(s, 10); }; var lines = input.split('\n'); var T = parse_fun(lines.shift()); for (var i=0;i<T;++i) { lines.shift(); var data = lines.shift().split(' ').map(parse_fun); check(data); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });Sherlock and Array Python Solutionif __name__ == "__main__": t = int(input()) for asdfs in range(t): n = int(input()) tab = list(map(int, input().split())) left = 0 right = 0 index = 0 i = 0 j = n-1 while True: if index == n-1: if left == right: print("YES") else: print("NO") break if left > right: right = right + tab[j] j = j - 1 else: left = left + tab[i] i = i + 1 index = index + 1 c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython