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:
int solution(vector<int> &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 C++
Time spent on task 4 minutes
Notes
not defined yet
Task timeline
Code: 16:20:37 UTC,
java,
autosave
Code: 16:20:39 UTC,
cpp,
autosave
Code: 16:20:53 UTC,
cpp,
autosave
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > i / 2)
result++;
}
return result;
}
Code: 16:20:57 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > i / 2)
result++;
}
return result;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got 3 expected 2
Code: 16:21:20 UTC,
cpp,
autosave
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > i / 2)
result++;
}
return result;
}
Code: 16:22:43 UTC,
cpp,
autosave
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > i / 2){
result++;
}
return result;
}
Code: 16:22:53 UTC,
cpp,
autosave
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > i / 2){
result++;
cout <<
}
}
return result;
}
Code: 16:22:59 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > i / 2){
result++;
cout << m[index] << endl;
}
}
return result;
}
Analysis
expand all
Example tests
1.
0.001 s
WRONG ANSWER,
got 3 expected 2
stdout:
3 3 2
Code: 16:23:42 UTC,
cpp,
autosave
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > (i / 2){
result++;
cout << m[index] << endl;
}
}
return result;
}
Code: 16:23:46 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > (i + 1) / 2){
result++;
cout << m[index] << endl;
}
}
return result;
}
Analysis
Code: 16:23:55 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > (i + 1) / 2)
result++;
}
return result;
}
Analysis
Code: 16:23:59 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > (i + 1) / 2)
result++;
}
return result;
}
Analysis
Code: 16:24:02 UTC,
cpp,
final,
score: 
100
// you can use includes, for example:
#include <map>
#include <iostream>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
map<int, int> m;
int max = 0, index, result = 0, count = 0;
for(int i = 0; i < A.size(); i++){
if(m.find(A[i]) != m.end()){
m[A[i]] += 1;
if(max < m[A[i]]){
max = m[A[i]];
index = A[i];
}
}else
m.insert(pair<int, int>(A[i], 1));
}
if(max < A.size() / 2)
return 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == index){
count++;
m[index]--;
}
if(m[index] > (A.size() - (i + 1)) / 2 && count > (i + 1) / 2)
result++;
}
return result;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK