Hackerrank Sherlock and array solution

The below program is the solution of Sherlock and array in hackerrank.

Screenshot[Demo] : 



Code :


#include<iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x,l=0,r=0,flag=0;
        cin>>x;
        int a[x];
        for(int i=0;i<x;i++)
        {
            cin>>a[i];
            r+=a[i];
        }
        for(int i=0;i<x;i++)
        {
            l=l+a[i-1];
            r=r-a[i];
            cout<<l<<" "<<r<<" "<<i<<endl;
            if(l==r)
            {
                flag=1;
                break;
            }

        }
        if(flag==1)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
}

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java