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


先求出数组总的sum,然后遍历一遍数组,遍历的过程中分别累加(减)求出一个元素的左边元素之和left(left的累加在这个元素上一个元素的循环中完成,见代码第30行)和右边元素之和right,然后比较是否相等即可,时间复杂度O(n),代码如下:

 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){
int n = in.nextInt();
int[] num = new int[n];
int sum = 0; for(int i = 0;i < n;i++){
num[i] = in.nextInt();
sum += num[i];
}
int left = 0;
int right = sum;
boolean find = false;
for(int i = 0;i < n;i++){
right -= num[i];
if(left == right)
{
find = true;
break;
}
left += num[i];
}
if(find)
System.out.println("YES");
else
System.out.println("NO");
}
}
}

【HackerRank】Sherlock and Array的更多相关文章

  1. 【HackerRank】Sherlock and MiniMax

    题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer  ...

  2. 【HackerRank】 Sherlock and The Beast

    Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...

  3. 【02】[].slice和Array.prototype.slice

    [02][].slice和Array.prototype.slice 01,Array是一个构造函数.浏览器内置的特殊对象.   02,Array没有slice方法. 03,Array.prototy ...

  4. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  5. 【leetcode81】Product of Array Except Self

    题目描述: 给定一个长度为n的整数数组Array[],输出一个等长的数组result[],这个输出数组,对应位置i是除了Array[i]之外,其他的所有元素的乘积 例如: given [1,2,3,4 ...

  6. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  7. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  8. 【leetcode】Merge Sorted Array

    题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...

  9. 【转载】Java集合类Array、List、Map区别和联系

    Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...

随机推荐

  1. hdu5800 To My Girlfriend dp 需要比较扎实的dp基础。

    To My Girlfriend Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. DJI SDK iOS 开发之二:搭建主要的开发环境

    本文想介绍搭建主要的DJI SDK iOS下的开发环境,只是DJI官方已经给出了非常具体的执行其demo的教程.网址例如以下: https://dev.dji.com/cn/guide 我这里总结一下 ...

  3. Centos 7 启动错误:XFS_WANT_CORRUPTED_GOTO 修复

    参考源 如果出现以下报错 [sda] Assuming drive cache: write through Internal error xfs XFS_WANT_CORRUPTED_GOTO at ...

  4. Linux 比较重要且难掌握命令 集合

    1. find find path –option [-print] [-exec command] {} \; find . -maxdepth 1 -name aa find . -maxdept ...

  5. Web 层由 Web,Web-MVC,Web-Socket 和 Web-Portlet 组成

    Web 层由 Web,Web-MVC,Web-Socket 和 Web-Portlet 组成,它们的细节如下: Web 模块提供面向web的基本功能和面向web的应用上下文,比如多部分(multipa ...

  6. Linq源代码阅读

    在 System.Core dotnet451\source\ndp\fx\src\core\system\linq\enumerable.cs Where 和 Select 内 ,把数组和List分 ...

  7. vim-addon-manager【转】

    Vim是一个非常优秀的编辑器,但是没装插件的Vim就始终只是个编辑器而已,是插件让Vim变得更加强大. 但是插件装得多了,管理就成了问题,Vim本身并没有提供插件管理功能,往往时间一长,.vim/vi ...

  8. iOS-毛玻璃、navigationBar滑动颜色渐变

    1.毛玻璃实现 iOS8以上,官方提供了系统方法实现毛玻璃,代码如下: // iOS8 UIVisualEffectView UIImageView *bgView = [[UIImageView a ...

  9. 服务器之FRU

    EEPROM是server主板上的电可擦除可编程只读存储器, 里面存储了FRU data, 包括制造商,产品型号,产品序列号,资产序列号等信息,为厂商和客户提供资产信息管理. 所以正确的FRU格式以及 ...

  10. linux之shell之if、while、for语句介绍

    一.基本判断条件 1)逻辑运算符 -a    expr1 -a expr2    逻辑与 -o    expr1 -o expr2    逻辑或 !     !expr1                ...