Tasks Details
easy
1.
EquiLeader
Find the index S such that the leaders of the sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N - 1] are the same.
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given.
The leader of this array is the value that occurs in more than half of the elements of A.
An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N − 1] have leaders of the same value.
For example, given array A such that:
A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2we can find two equi leaders:
- 0, because sequences: (4) and (3, 4, 4, 4, 2) have the same leader, whose value is 4.
- 2, because sequences: (4, 3, 4) and (4, 4, 2) have the same leader, whose value is 4.
The goal is to count the number of equi leaders.
Write a function:
def solution(A)
that, given a non-empty array A consisting of N integers, returns the number of equi leaders.
For example, given:
A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2the function should return 2, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−1,000,000,000..1,000,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 18 minutes
Notes
not defined yet
Code: 18:07:47 UTC,
java,
autosave
Code: 18:09:50 UTC,
py,
autosave
Code: 18:10:08 UTC,
py,
autosave
Code: 18:10:18 UTC,
py,
autosave
Code: 18:12:40 UTC,
py,
autosave
Code: 18:12:49 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or :
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
print(candidate)
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 70, in main sol = __import__('solution') File "/tmp/solution.py", line 8 if len(candidate) == 0 or : ^ SyntaxError: invalid syntax
Code: 18:13:00 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
print(candidate)
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[4, 4]
Code: 18:13:09 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
for
Code: 18:13:19 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
for candidate[0]
Code: 18:13:30 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
Code: 18:14:03 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
p
Code: 18:14:19 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
print(A.count(candidate[0]))
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
4
Code: 18:14:33 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
count = A.count(candidate[0]))
Code: 18:14:58 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
count = A.count(candidate[0])
if count <= len(A) // 2:
return 0
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
Code: 18:15:07 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
count = A.count(candidate[0])
if count <= len(A) // 2:
return 0
Code: 18:15:13 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
count = A.count(candidate[0])
if count <= len(A) // 2:
return 0
User test case 1:
[4]
User test case 2:
[1, 2]
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
expand all
User tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 13, in solution count = A.count(candidate[0]) IndexError: list index out of range
Code: 18:15:24 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
count = A.count(candidate[0])
if count <= len(A) // 2:
return 0
Code: 18:15:49 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
for i in A:
Code: 18:16:19 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
Code: 18:16:41 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
Code: 18:17:01 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
Code: 18:17:20 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1 = 0
p2 = len(A)
Code: 18:17:49 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
Code: 18:17:59 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
for
Code: 18:18:10 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
for i,v in enumerate(A):
Code: 18:18:34 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if
Code: 18:19:04 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len
Code: 18:19:33 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
User test case 1:
[4]
User test case 2:
[1, 2]
Analysis
Code: 18:19:39 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
Code: 18:20:09 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
Code: 18:20:14 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
User test case 1:
[4]
User test case 2:
[1, 2]
User test case 3:
[4, 4, 4, 4]
User test case 4:
[1, 1, 1, 1, 4]
User test case 5:
[1, 1, 1, 1, 4, 4]
User test case 6:
[4, 4, 4, 4, 1, 2, 3, 4, 4, 4, 4]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 3
function result: 3
1.
0.036 s
OK
function result: 2
function result: 2
1.
0.036 s
OK
function result: 1
function result: 1
1.
0.036 s
OK
function result: 10
function result: 10
Code: 18:21:43 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
Code: 18:21:51 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
print(candidate)
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
User test case 1:
[4]
User test case 2:
[1, 2]
User test case 3:
[4, 4, 4, 4]
User test case 4:
[1, 1, 1, 1, 4]
User test case 5:
[1, 1, 1, 1, 4, 4]
User test case 6:
[4, 4, 4, 4, 1, 2, 3, 4, 4, 4, 4]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: 0
function result: 0
stdout:
[4]
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 3
function result: 3
stdout:
[4, 4, 4, 4]
1.
0.036 s
OK
function result: 2
function result: 2
stdout:
[1, 1, 1]
1.
0.036 s
OK
function result: 1
function result: 1
stdout:
[1, 1]
1.
0.036 s
OK
function result: 10
function result: 10
stdout:
[4, 4, 4, 4, 4]
Code: 18:22:09 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
print(candidate)
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
print("i:",i)
equi += 1
return equi
User test case 1:
[4]
User test case 2:
[1, 2]
User test case 3:
[4, 4, 4, 4]
User test case 4:
[1, 1, 1, 1, 4]
User test case 5:
[1, 1, 1, 1, 4, 4]
User test case 6:
[4, 4, 4, 4, 1, 2, 3, 4, 4, 4, 4]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: 0
function result: 0
stdout:
[4]
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 3
function result: 3
stdout:
[4, 4, 4, 4] i: 0 i: 1 i: 2
1.
0.036 s
OK
function result: 2
function result: 2
stdout:
[1, 1, 1] i: 0 i: 1
1.
0.036 s
OK
function result: 1
function result: 1
stdout:
[1, 1] i: 0
1.
0.036 s
OK
function result: 10
function result: 10
stdout:
[4, 4, 4, 4, 4] i: 0 i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7 i: 8 i: 9
Code: 18:23:06 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
print(candidate)
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
print("i:",i)
equi += 1
return equi
Code: 18:23:09 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
print(candidate)
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
print("i:",i)
equi += 1
return equi
User test case 1:
[4]
User test case 2:
[1, 2]
User test case 3:
[1, 1]
User test case 4:
[4, 4, 4, 4]
User test case 5:
[1, 1, 1, 1, 4]
User test case 6:
[1, 1, 1, 1, 4, 4]
User test case 7:
[4, 4, 4, 4, 1, 2, 3, 4, 4, 4, 4]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: 0
function result: 0
stdout:
[4]
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 1
function result: 1
stdout:
[1, 1] i: 0
1.
0.036 s
OK
function result: 3
function result: 3
stdout:
[4, 4, 4, 4] i: 0 i: 1 i: 2
1.
0.036 s
OK
function result: 2
function result: 2
stdout:
[1, 1, 1] i: 0 i: 1
1.
0.036 s
OK
function result: 1
function result: 1
stdout:
[1, 1] i: 0
1.
0.036 s
OK
function result: 10
function result: 10
stdout:
[4, 4, 4, 4, 4] i: 0 i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7 i: 8 i: 9
Code: 18:24:27 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
print("i:",i)
equi += 1
return equi
Code: 18:24:39 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
Code: 18:24:40 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
User test case 1:
[4]
User test case 2:
[1, 2]
User test case 3:
[1, 1]
User test case 4:
[4, 4, 4, 4]
User test case 5:
[1, 1, 1, 1, 4]
User test case 6:
[1, 1, 1, 1, 4, 4]
User test case 7:
[4, 4, 4, 4, 1, 2, 3, 4, 4, 4, 4]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 1
function result: 1
1.
0.036 s
OK
function result: 3
function result: 3
1.
0.036 s
OK
function result: 2
function result: 2
1.
0.036 s
OK
function result: 1
function result: 1
1.
0.036 s
OK
function result: 10
function result: 10
Code: 18:24:47 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
User test case 1:
[4]
User test case 2:
[1, 2]
User test case 3:
[1, 1]
User test case 4:
[4, 4, 4, 4]
User test case 5:
[1, 1, 1, 1, 4]
User test case 6:
[1, 1, 1, 1, 4, 4]
User test case 7:
[4, 4, 4, 4, 1, 2, 3, 4, 4, 4, 4]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 0
function result: 0
1.
0.036 s
OK
function result: 1
function result: 1
1.
0.036 s
OK
function result: 3
function result: 3
1.
0.056 s
OK
function result: 2
function result: 2
1.
0.056 s
OK
function result: 1
function result: 1
1.
0.056 s
OK
function result: 10
function result: 10
Code: 18:24:51 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
candidate = []
for i,v in enumerate(A):
if len(candidate) == 0 or v == candidate[-1]:
candidate.append(v)
elif v != candidate[-1]:
candidate.pop()
try:
count = A.count(candidate[0])
except:
count = 0
if count <= len(A) // 2:
return 0
p1_len = 0
p2_len = len(A)
p1_count = 0
p2_count = count
equi = 0
for i,v in enumerate(A):
p1_len += 1
p2_len -= 1
if v == candidate[0]:
p1_count += 1
p2_count -= 1
if p1_count > p1_len // 2 and p2_count > p2_len // 2:
equi += 1
return equi
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK