【HackerRank】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
先求出数组总的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的更多相关文章
- 【HackerRank】Sherlock and MiniMax
题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer ...
- 【HackerRank】 Sherlock and The Beast
Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...
- 【02】[].slice和Array.prototype.slice
[02][].slice和Array.prototype.slice 01,Array是一个构造函数.浏览器内置的特殊对象. 02,Array没有slice方法. 03,Array.prototy ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【leetcode81】Product of Array Except Self
题目描述: 给定一个长度为n的整数数组Array[],输出一个等长的数组result[],这个输出数组,对应位置i是除了Array[i]之外,其他的所有元素的乘积 例如: given [1,2,3,4 ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【leetcode】Merge Sorted Array
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...
- 【转载】Java集合类Array、List、Map区别和联系
Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...
随机推荐
- Alluxio部署(local模式)
下载(pre-build for Hadoop 2.7) http://www.alluxio.org/download` 解压 tar -xvf alluxio-1.3.0-hadoop2.7-bi ...
- Ubuntu17 root ssh
Ubuntu provides OpenSSH (OpenBSD Secure Shell) in its universe repositories, which is a suite of sec ...
- docker导入导出
导出镜像 docker save -o centos7.tar centos # 导入本地镜像 docker load --input centos7.tar docker ps -a docker ...
- 第一百九十二节,jQuery EasyUI 使用
jQuery EasyUI 使用 学习要点: 1.引入必要的文件 2.加载 UI 组件的方式 3.使用 easyload.js 智能加载 4.Parser 解析器 本节课重点了解 EasyUI 的两种 ...
- 结果集(ResultSet)用法
结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数据的更新等. 结果集读取数据 ...
- 解决OV系列摄像头寄存器读数据无法收到的问题
最近工作中接了一款OV7725的sensor,由于平台已经接过很多的家的sensor也就没有太当回事.问题出现的很奇怪,再看了 register map后基本确定了要尽心register R/W测试 ...
- python socket编程入门(编写server实例)+send 与sendall的区别与使用方法
python 编写server的步骤: 1. 第一步是创建socket对象.调用socket构造函数.如: socket = socket.socket( family, type ) family参 ...
- C++编译过程与内存空间
为什么须要知道C/C++的内存布局和在哪能够能够找到想要的数据?知道内存布局对调试程序很有帮助,能够知道程序运行时,究竟做了什么,有助于写出干净的代码.本文的主要内容例如以下: 源文件转换为可运 ...
- pycharn设置git提交代码
1.设置pycharm的git地址 2.设置git地址及本地路径 3.提交代码
- SharePoint服务器端对象模型 之 访问网站和列表数据(Part 5)
(五)列表条目(SPListItem) SharePoint中数据的存储基本上都是通过列表条目来完成(文档库中的文档也是一种特殊的列表条目),因此在SharePoint应用开发中,最终是要和列表条目打 ...