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:
class Solution { public int solution(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 15 minutes
Notes
not defined yet
Task timeline
Code: 08:51:33 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
int equiLeaders=0;
int leaders=0;
for (int i=0; i<n; i++)
{
if (A[i] == leader) leaders++;
if (leaders > (i+1)/2 && leaders > (n-1-i)/2) equiLeaders++;
}
return equiLeaders;
}
}
Analysis
expand all
Example tests
1.
0.066 s
WRONG ANSWER,
got 4 expected 2
Code: 08:56:37 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
int equiLeaders=0;
int leaders=0;
for (int i=0; i<n; i++)
{
if (A[i] == leader) leaders++;
if (leaders > (i+1)/2 && leaders > (n-1-i)/2) equiLeaders+=1;
}
return equiLeaders;
}
}
Analysis
expand all
Example tests
1.
0.064 s
WRONG ANSWER,
got 4 expected 2
Code: 08:58:59 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
int equiLeaders=0;
int leaders=0;
for (int i=0; i<n; i++)
{
if (A[i] == leader) leaders++;
if (leaders > (i+1)/2 && count -leaders > (n-1-i)/2) equiLeaders++;
}
return equiLeaders;
}
}
Analysis
Code: 09:03:50 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
int equiLeaders=0;
int leaders=0;
for (int i=0; i<n; i++)
{
if (A[i] == leader) leaders++;
if (leaders > (i+1)/2 && count-leaders > (n-1-i)/2) equiLeaders++;
}
return equiLeaders;
}
}
Analysis
Code: 09:03:52 UTC,
cs,
final,
score: 
100
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
int equiLeaders=0;
int leaders=0;
for (int i=0; i<n; i++)
{
if (A[i] == leader) leaders++;
if (leaders > (i+1)/2 && count-leaders > (n-1-i)/2) equiLeaders++;
}
return equiLeaders;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.066 s
OK
2.
0.065 s
OK
3.
0.065 s
OK
1.
0.066 s
OK
2.
0.065 s
OK
3.
0.066 s
OK
1.
0.066 s
OK
2.
0.065 s
OK
1.
0.065 s
OK
1.
0.066 s
OK