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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 5 minutes
Notes
not defined yet
Task timeline
Code: 01:18:44 UTC,
java,
autosave
Code: 01:19: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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
Code: 01:19:37 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
Code: 01:19:47 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
Code: 01:20:25 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
p
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
Code: 01:20:37 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
print(curNum, i, num, leader)
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
Code: 01:20:37 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
print(curNum, i, num, leader)
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
User test case 1:
[4, 4, 2, 5, 3, 4, 4, 4]
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
1 0 4 4 2 2 4 4 3 3 4 4 4 4 4 4
expand all
User tests
1.
0.036 s
OK
function result: 2
function result: 2
stdout:
1 0 5 4 2 1 5 4 3 5 5 4 4 6 5 4 5 7 5 4
Code: 01:21:06 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
print(curNum, i, num, leader, Sum)
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
User test case 1:
[4, 4, 2, 5, 3, 4, 4, 4]
Analysis
expand all
Example tests
1.
0.056 s
OK
stdout:
1 0 4 4 0 2 2 4 4 1 3 3 4 4 2 4 4 4 4 2
expand all
User tests
1.
0.060 s
OK
function result: 2
function result: 2
stdout:
1 0 5 4 0 2 1 5 4 1 3 5 5 4 1 4 6 5 4 1 5 7 5 4 2
Code: 01:23:00 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
print(curNum, i, num, leader, Sum)
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
Code: 01:23:10 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
User test case 1:
[4, 4, 2, 5, 3, 4, 4, 4]
Analysis
Code: 01:23:31 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
User test case 1:
[4, 4, 2, 5, 3, 4, 4, 4]
Analysis
Code: 01:23:33 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
num, leader = 1, A[0]
for a in A[1:]:
if a == leader:
num += 1
elif num > 0:
num -= 1
else:
num = 1
leader = a
num = A.count(leader)
if num <= len(A) / 2:
return 0
curNum = 0
Sum = 0
for i, a in enumerate(A):
if a == leader:
curNum += 1
if curNum > (i+1) / 2 and num - curNum > (len(A)-i-1) / 2:
Sum += 1
return Sum
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.040 s
OK
2.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK