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. 着手打造你的随身系统---将linux装进移动硬盘

    将Ubuntu等linux系统安装到移动硬盘--操作系统随身携带 前言        刚刚接触ubuntu,听说可以将linux系统安装到移动硬盘上,所以最近一周都在尝试将ubuntu安装到新买的移动 ...

  2. mediawiki常用设置

    修改mediawiki的logo图标 在LocalSettings.php文件中添加或修改以下代码: //$wgLogo             = "$wgStylePath/common ...

  3. 第一百六十九节,jQuery,基础事件

    jQuery,基础事件 学习要点: 1.绑定事件 2.简写事件 3.复合事件 JavaScript 有一个非常重要的功能,就是事件驱动.当页面完全加载后,用户通过鼠标 或键盘触发页面中绑定事件的元素即 ...

  4. Hibernate使用xml文件的每个类层次一张表

    通过这种继承策略,我们可以通过单表映射整个层次结构. 这里,在表中创建一个额外的列(也称为discriminator列)来标识该类. 让我们先了解问题.下面给出的整个层次类映射到数据库的一个表中图解说 ...

  5. 百度-设置-搜索设置-每页显示50条-保存设置-打印alert信息-accept确定

    一.场景: 代码: #coding:utf-8from selenium import webdriverfrom selenium.webdriver.common.action_chains im ...

  6. day1笔记 初识python,paython基础

    一.计算机,操作系统 软件发送指令给操作系统,操作系统再把指令发送给  内存,cpu,硬盘等 二.Python的历史. Python2: 1.臃肿,源码的重复量很多.2.语法不清晰,掺杂着c,++,P ...

  7. 三维空间直线最近点对hdu4741

    //求两条直线之间的关系(三维) //输入:两条不为点的直线 //输出:相交返回XIANGJIAO和交点p,平行返回PINGXING,共线返回GONGXIAN int LineAndLine(Line ...

  8. code first 数据库无损迁移

    环境:vs2013+nuget Enable-Migrations -EnableAutomaticMigrations Update-Database

  9. commit Commit changes to stable storage 对变化提交

    Python36\site-packages\pymysql\connections.py # Python implementation of the MySQL client-server pro ...

  10. 【转】 HMC与VIOS对新LPAR提供存储与网络虚拟化的支持

    前面的几篇博文的操作环境都是在IVM下,IVM可以看作是VIOS的一部分,或者是对VIOS功能的一个扩展,一个IVM只能管理1台物理服务器,而HMC则是一对多.在有HMC来管理物理服务器的情形下,VI ...