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].
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
if A[:i + 1].count(lead_num) > (i+1) / 2 and A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
if A[:i + 1].count(lead_num) > (i+1) / 2
A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
if A[:i + 1].count(lead_num) > (i+1) / 2 and A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
if A[:i + 1].count(lead_num) > (i+1) / 2
and A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
for
#if A[:i + 1].count(lead_num) > (i+1) / 2
#and A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
# answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
temp_count = 0
for num in tempp
#if A[:i + 1].count(lead_num) > (i+1) / 2
#and A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
# answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
temp_count = 0
for num in A[:i + 1]:
#if A[:i + 1].count(lead_num) > (i+1) / 2
#and A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
# answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
t
#if A[:i + 1].count(lead_num) > (i+1) / 2
#and A[i+1 : ].count(lead_num) > (a_length - i - 1) / 2:
# answer += 1
return answer
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
t
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
first_count >
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
if num ==
first_count >
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
if num == lead_num:
first_count >
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
if num == lead_num:
first_count >
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
if num == lead_num:
first_count > i
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
for num in A[:i + 1]:
if num == lead_num:
first_count > i + 1
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_fi
for num in A[:i + 1]:
if num == lead_num:
first_count > i + 1
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count > i + 1
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count > i + 1
if
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count > i + 1
if first_count > i:
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
if first_count > i:
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
if first_count > i + :
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
if first_count > i + 1:
is_first
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
if first_count > i + 1:
is_first = True
if is_first:
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
if first_count > i + 1:
is_first = True
if is_first:
for num in A[]
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
if first_count > i + 1:
is_first = True
if is_first:
for num in A[i + 1:]:
if num == lead_num:
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
if first_count > i + 1:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > i + 1:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
secound_count += 1
if se
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > i + 1:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
secound_count += 1
if second_count > (a_length - i - 1)
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
secound_count += 1
if second_count > (a_length - i - 1) / 2:
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
secound_count += 1
if second_count > (a_length - i - 1) / 2:
if_second
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
secound_count += 1
if second_count > (a_length - i - 1) / 2:
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
secound_count += 1
if second_count > (a_length - i - 1) / 2:
answer += 1
continue
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
second_count += 1
if second_count > (a_length - i - 1) / 2:
answer += 1
continue
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
second_count += 1
if second_count > (a_length - i - 1) / 2:
answer += 1
continue
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
second_count += 1
if second_count > (a_length - i - 1) / 2:
answer += 1
continue
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
second_count += 1
if second_count > (a_length - i - 1) / 2:
answer += 1
continue
else:
continue
return answer
import collections
def solution(A):
a_length = len(A)
a_hash = collections.defaultdict(int)
lead_num = 0
answer=0
for i, num in enumerate(A):
a_hash[num] += 1
if a_hash[num] > a_length/2:
lead_num = num
break
for i , _ in enumerate(A[:-1]):
first_count = 0
is_first = False
for num in A[:i + 1]:
if num == lead_num:
first_count += 1
if first_count > (i + 1) / 2:
is_first = True
if is_first:
second_count = 0
for num in A[i + 1:]:
if num == lead_num:
second_count += 1
if second_count > (a_length - i - 1) / 2:
answer += 1
continue
else:
continue
return answer
The following issues have been detected: wrong answers, timeout errors.
large random test with two values, length = ~50,000
Killed. Hard limit reached: 6.000 sec.
random(0,1) + 50000 * [0] + random(0, 1), length = ~100,000
Killed. Hard limit reached: 6.000 sec.
1, 2, ..., N, length = ~100,000
Killed. Hard limit reached: 6.000 sec.