-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.py
More file actions
90 lines (70 loc) · 1.93 KB
/
new.py
File metadata and controls
90 lines (70 loc) · 1.93 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the balancedSums function below.
def balancedSums(arr):
res=[]
if len(arr)==1:
return "YES"
for i in range(len(arr)):
leftsum=0
rightsum=0
if i==0:
k=1
while k<len(arr):
rightsum+=arr[k]
k+=1
if rightsum==0:
return "YES"
elif i==len(arr)-1:
leftsum=0
rightsum=0
k=0
while k<len(arr)-1:
leftsum+=arr[k]
k+=1
if leftsum==0:
return "YES"
else:
return "NO"
else:
start=0
end=len(arr)-1
leftsum=0
rightsum=0
leftsum+=arr[start]
rightsum+=arr[end]
while start<=end:
if leftsum<rightsum:
start+=1
leftsum+=arr[start]
elif leftsum>rightsum:
end+=1
rightsum+=arr[end]
else:
if start+2==end:
return "YES"
start+=1
# j=0
# k=i+1
# rightsum=0
# leftsum=0
# while j<i:
# leftsum+=arr[j]
# j+=1
# while k<len(arr):
# rightsum+=arr[k]
# k+=1
# if leftsum==rightsum:
# return "YES"
# break
if __name__ == '__main__':
T = int(input().strip())
for T_itr in range(T):
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
result = balancedSums(arr)
print(result)