Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • 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

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Sherlock and Array problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes