Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Sherlock and Array problem solution

YASH PAL, 31 July 202430 November 2025

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.

HackerRank Sherlock and Array problem solution

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)

        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())

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();

            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;

    }
}

{“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("YESn");
            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("YESn");
            else printf("NOn");
        }
    }
}

{“mode”:”full”,”isActive”:false}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes