PROBLEM (https://www.hackerrank.com/challenges/sherlock-and-array)
Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum is considered to be zero. Formally, find an i, such that, A1+A2...Ai-1 = Ai+1+Ai+2...AN.
Input Format
The first line contains T, the number of test cases. For each test case, the first line contains N, the number of elements in the array
A. The second line for each testcase contains N space separated integers, denoting the array A.
Output Format
For each test case, print YES if there
exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right, else print NO.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤ 105
1 ≤ Ai ≤ 2*104 for 1 ≤ i ≤ N
Sample Input
2
3
1 2 3
4
1 2 3 3
Sample Output
NO
YES
Explanation
For test case 1, no such index exists.
For test case 2, A[1]+A[2]=A[4], therefore index 3 satisfies.
SOLUTION
#include<stdio.h>
int main(){
int T,t,n,i,j;
scanf("%d",&T);
for(t=0;t<T;t++){
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
int first=0,last=0;
for(i=1;i<n;i++){
last+=a[i];
}
for(i=0;i<n-1;i++){
if(first==last){
printf("YES\n");
break;
}
first+= a[i];
last-= a[i+1];
}
if(i==n-1 && n!=1)
printf("NO\n");
if(n==1)
printf("YES\n");
}
return 0;
}
Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum is considered to be zero. Formally, find an i, such that, A1+A2...Ai-1 = Ai+1+Ai+2...AN.
Input Format
The first line contains T, the number of test cases. For each test case, the first line contains N, the number of elements in the array
A. The second line for each testcase contains N space separated integers, denoting the array A.
Output Format
For each test case, print YES if there
exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right, else print NO.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤ 105
1 ≤ Ai ≤ 2*104 for 1 ≤ i ≤ N
Sample Input
2
3
1 2 3
4
1 2 3 3
Sample Output
NO
YES
Explanation
For test case 1, no such index exists.
For test case 2, A[1]+A[2]=A[4], therefore index 3 satisfies.
SOLUTION
#include<stdio.h>
int main(){
int T,t,n,i,j;
scanf("%d",&T);
for(t=0;t<T;t++){
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
int first=0,last=0;
for(i=1;i<n;i++){
last+=a[i];
}
for(i=0;i<n-1;i++){
if(first==last){
printf("YES\n");
break;
}
first+= a[i];
last-= a[i+1];
}
if(i==n-1 && n!=1)
printf("NO\n");
if(n==1)
printf("YES\n");
}
return 0;
}
if u want any solution in hacker rank....just write the question name of hacker rank site here i will post the solution....
ReplyDeletesherlock and array
Deletefibonaci of anumber
Deleteodd numbers between l and R
DeleteAll solutions are available in this link
Deletehttp://karthikalapati.blogspot.com/2017/04/hacker-rank-solutions.html
do have a java code sir?
Deletedo you have java code
Deletehttp://karthikalapati.blogspot.in/2016/03/hacker-rank-solution-for-sherlock-and.html
ReplyDeleteSherlock numbers between 0toN
ReplyDelete