【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),代码如下:

 1 import java.io.*;
 2 import java.util.*;
 3 import java.text.*;
 4 import java.math.*;
 5 import java.util.regex.*;
 6
 7 public class Solution {
 8     public static void main(String[] args) {
 9         Scanner in = new Scanner(System.in);
10         int t = in.nextInt();
11         while(t-- > 0){
12             int n = in.nextInt();
13             int[] num = new int[n];
14             int sum = 0;
15
16             for(int i = 0;i < n;i++){
17                 num[i] = in.nextInt();
18                 sum += num[i];
19             }
20             int left = 0;
21             int right = sum;
22             boolean find = false;
23             for(int i = 0;i < n;i++){
24                 right -= num[i];
25                 if(left == right)
26                 {
27                     find = true;
28                     break;
29                 }
30                 left += num[i];
31             }
32             if(find)
33                 System.out.println("YES");
34             else
35                 System.out.println("NO");
36         }
37       }
38 }

【HackerRank】Sherlock and Array

时间: 2024-08-08 17:45:07

【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 M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

【HackerRank】 Sherlock and The Beast

Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has b

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the

【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

QuickSort In the previous challenge, you wrote a partition method to split an array into 2 sub-arrays, one containing smaller elements and one containing larger elements. This means you 'sorted' half the array with respect to the other half. Can you

【HackerRank】Find the Median(Partition找到数组中位数)

In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific information about a list of numbers, and doing a full sort would be unnecessary. Can you figure out a way to use your partition code to find the median in an a

【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, arrays may be too large for us to wait around for insertion sort to finish. Is there some other way we can calculate the number of times Insertion Sort

【HackerRank】Closest Numbers

Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses also. Challenge Given a list of unsorted numbers, can you find the numbers that have the smallest absol

【HackerRank】Service Lane

Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the

【HackerRank】Lonely Integer

There are N integers in an array A. All but one integer occur in pairs. Your task is to find out the number that occurs only once. Input Format The first line of the input contains an integer N indicating number of integers. The next line contains N