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 3 minutes
Notes
not defined yet
Task timeline
Code: 11:25:14 UTC,
java,
autosave
Code: 11:25:20 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int max_val = 0;
int leader;
int res = 0;
map<int, int> leader_cnt;
for(int i=0; i<len; i++){
if(leader_cnt.find(A[i]) == leader_cnt.end()){
leader_cnt[A[i]] = 1;
}else{
leader_cnt[A[i]]++;
if(max_val < leader_cnt[A[i]]){
max_val = leader_cnt[A[i]];
leader = A[i];
}
}
}
if(max_val < len/2 ){
return 0;
}
int left_cnt = 0;
int right_cnt = leader_cnt[A[leader]];
for(int i=0; i<len; i++){
if(A[i] == leader){
left_cnt++;
right_cnt--;
}
if(right_cnt > (len-(i+1))/2 && left_cnt > (i+1)/2){
res++;
}
}
return res;
}
Code: 11:25:36 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int max_val = 0;
int leader;
int res = 0;
map<int, int> leader_cnt;
// 1 element
for(int i=0; i<len; i++){
if(leader_cnt.find(A[i]) == leader_cnt.end()){
leader_cnt[A[i]] = 1;
}else{
leader_cnt[A[i]]++;
if(max_val < leader_cnt[A[i]]){
max_val = leader_cnt[A[i]];
leader = A[i];
}
}
}
if(max_val < len/2 ){
return 0;
}
int left_cnt = 0;
int right_cnt = leader_cnt[A[leader]];
for(int i=0; i<len; i++){
if(A[i] == leader){
left_cnt++;
right_cnt--;
}
if(right_cnt > (len-(i+1))/2 && left_cnt > (i+1)/2){
res++;
}
}
return res;
}
Code: 11:26:01 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int max_val = 0;
int leader;
int res = 0;
map<int, int> leader_cnt;
// 1 element
if(len == 1){
return 0;
}
// several elements
for(int i=0; i<len; i++){
if(leader_cnt.find(A[i]) == leader_cnt.end()){
leader_cnt[A[i]] = 1;
}else{
leader_cnt[A[i]]++;
if(max_val < leader_cnt[A[i]]){
max_val = leader_cnt[A[i]];
leader = A[i];
}
}
}
if(max_val < len/2 ){
return 0;
}
int left_cnt = 0;
int right_cnt = leader_cnt[A[leader]];
for(int i=0; i<len; i++){
if(A[i] == leader){
left_cnt++;
right_cnt--;
}
if(right_cnt > (len-(i+1))/2 && left_cnt > (i+1)/2){
res++;
}
}
return res;
}
Code: 11:27:30 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int max_val = 0;
int leader;
int res = 0;
map<int, int> leader_cnt;
// 1 element
if(len == 1){
return 0;
}
// several elements
for(int i=0; i<len; i++){
if(leader_cnt.find(A[i]) == leader_cnt.end()){
leader_cnt[A[i]] = 1;
}else{
leader_cnt[A[i]]++;
if(max_val < leader_cnt[A[i]]){
max_val = leader_cnt[A[i]];
leader = A[i];
}
}
}
if(max_val < len/2 ){
return 0;
}
int left_cnt = 0;
int right_cnt = leader_cnt[leader];
for(int i=0; i<len; i++){
if(A[i] == leader){
left_cnt++;
right_cnt--;
}
if(right_cnt > (len-(i+1))/2 && left_cnt > (i+1)/2){
res++;
}
}
return res;
}
Code: 11:27:31 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int max_val = 0;
int leader;
int res = 0;
map<int, int> leader_cnt;
// 1 element
if(len == 1){
return 0;
}
// several elements
for(int i=0; i<len; i++){
if(leader_cnt.find(A[i]) == leader_cnt.end()){
leader_cnt[A[i]] = 1;
}else{
leader_cnt[A[i]]++;
if(max_val < leader_cnt[A[i]]){
max_val = leader_cnt[A[i]];
leader = A[i];
}
}
}
if(max_val < len/2 ){
return 0;
}
int left_cnt = 0;
int right_cnt = leader_cnt[leader];
for(int i=0; i<len; i++){
if(A[i] == leader){
left_cnt++;
right_cnt--;
}
if(right_cnt > (len-(i+1))/2 && left_cnt > (i+1)/2){
res++;
}
}
return res;
}
Analysis
Code: 11:27:42 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int max_val = 0;
int leader;
int res = 0;
map<int, int> leader_cnt;
// 1 element
if(len == 1){
return 0;
}
// several elements
for(int i=0; i<len; i++){
if(leader_cnt.find(A[i]) == leader_cnt.end()){
leader_cnt[A[i]] = 1;
}else{
leader_cnt[A[i]]++;
if(max_val < leader_cnt[A[i]]){
max_val = leader_cnt[A[i]];
leader = A[i];
}
}
}
if(max_val < len/2 ){
return 0;
}
int left_cnt = 0;
int right_cnt = leader_cnt[leader];
for(int i=0; i<len; i++){
if(A[i] == leader){
left_cnt++;
right_cnt--;
}
if(right_cnt > (len-(i+1))/2 && left_cnt > (i+1)/2){
res++;
}
}
return res;
}
Analysis
Code: 11:27:47 UTC,
cpp,
final,
score: 
100
// you can use includes, for example:
#include <bits/stdc++.h>
// 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)
int len = A.size();
int max_val = 0;
int leader;
int res = 0;
map<int, int> leader_cnt;
// 1 element
if(len == 1){
return 0;
}
// several elements
for(int i=0; i<len; i++){
if(leader_cnt.find(A[i]) == leader_cnt.end()){
leader_cnt[A[i]] = 1;
}else{
leader_cnt[A[i]]++;
if(max_val < leader_cnt[A[i]]){
max_val = leader_cnt[A[i]];
leader = A[i];
}
}
}
if(max_val < len/2 ){
return 0;
}
int left_cnt = 0;
int right_cnt = leader_cnt[leader];
for(int i=0; i<len; i++){
if(A[i] == leader){
left_cnt++;
right_cnt--;
}
if(right_cnt > (len-(i+1))/2 && left_cnt > (i+1)/2){
res++;
}
}
return res;
}
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