Sherlock and Array : HackerRank Problem Solution

Sherlock and Array : HackerRank Problem Solution


C Implementation:


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 1000000
int main() {

    int T,N,a[MAX],sum,i,left,flag;
    scanf("%d",&T);
    while(T--)
        {
        flag=0;
        sum=0;
        left=0;
        scanf("%d",&N);
        for(i=0;i<N;i++)
            {
            scanf("%d",(a+i));
            sum+=*(a+i);
        }
        for(i=0;i<N;i++)
            {
            sum=sum-*(a+i);
            if(left==sum)
                {
                flag=1;
                break;
            }
            left=left+*(a+i);
        }
        if(flag==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

C++ Implementation:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int t;
    cin>>t;
    while(t--)
        {
        bool flag = false;
        int sum = 0;
        int left = 0;
        int n;
        cin>>n;
        int a[n];
        for(int i = 0;i<n;i++)
            {
            cin>>a[i];
            sum += a[i];
        }
        
        for(int i=0;i<n;i++)
            {
            sum = sum - a[i];
            if(left==sum)
                {
                flag = true;
                break;
            }
            left = left + a[i];
        }
        
        if(flag)
                cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

Java Implementation:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while(t>0)
            {
            boolean flag = false;
            int sum = 0;
            int left = 0;
            int n = in.nextInt();
            int a[] = new int[n];
            for(int i=0;i<n;i++)
                {
                a[i] = in.nextInt();
                sum = sum + a[i];
            }
            
            for(int i=0;i<n;i++)
                {
                sum = sum - a[i];
                if(left==sum)
                    {
                    flag = true;
                    break;
                }
                left = left + a[i];
            }
            
            if(flag)
                System.out.println("YES");
            else
                System.out.println("NO");
            t--;
        }
        
    }
}

Python Implementation:

t = int(raw_input())
while t>0:
    flag=0
    sm=0
    left=0
    n = int(raw_input())
    lis = map(int,raw_input().split(' '))
    sm = sum(lis)
    for i in range(n):
        sm = sm - lis[i]
        if left==sm:
            flag=1
            break
        left = left + lis[i]
    if flag:
        print "YES"
    else:
        print "NO"
    t-=1