-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
65 lines (46 loc) · 1.5 KB
/
Solution.java
File metadata and controls
65 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package hackrank.algorithm.search.sherlock;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Sherlock and Array Challenge
*
* @see https://www.hackerrank.com/challenges/sherlock-and-array
*/
public class Solution {
public static void main(String[] args) {
List<List<Integer>> allNumbers = readInput();
for (List<Integer> numbers : allNumbers) {
System.out.println(hasEqualSumSplit(numbers) ? "YES" : "NO");
}
}
private static boolean hasEqualSumSplit(List<Integer> numbers) {
int total = numbers.stream().mapToInt(Integer::intValue).sum();
int leftSum = 0;
int rightSum = total;
for (int i = 0; i < numbers.size(); i++) {
int number = numbers.get(i);
rightSum -= number;
if (leftSum == rightSum) {
return true;
}
leftSum += number;
}
return false;
}
private static List<List<Integer>> readInput() {
Scanner scanner = new Scanner(System.in);
List<List<Integer>> input = new ArrayList<>();
int testCases = scanner.nextInt();
for (int i = 0; i < testCases; i++) {
List<Integer> numbers = new ArrayList<>();
int length = scanner.nextInt();
for (int j = 0; j < length; j++) {
numbers.add(scanner.nextInt());
}
input.add(numbers);
}
scanner.close();
return input;
}
}