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:
function 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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 17 minutes
Notes
not defined yet
Code: 10:03:39 UTC,
java,
autosave
Code: 10:08:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
if (stack.length > 0) {
const candidate = stack.pop();
const minCount = Math.floor(N / 2) + 1;
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
if (count >= minCount) {
return i;
}
}
}
}
return -1;
}
Code: 10:08:29 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
if (stack.length > 0) {
const candidate = stack.pop();
const minCount = Math.floor(N / 2) + 1;
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
if (count >= minCount) {
return i;
}
}
}
}
return -1;
}
Code: 10:08:56 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const candidate = stack.pop();
const minCount = Math.floor(N / 2) + 1;
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
if (count >= minCount) {
return i;
}
}
}
return -1;
}
Code: 10:09:42 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const candidate = stack.pop();
const minCount = Math.floor(N / 2) + 1;
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
}
}
return -1;
}
Code: 10:09:56 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
}
}
return -1;
}
Code: 10:10:06 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
}
}
//
}
Code: 10:10:24 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
}
}
// search for equi leaders
for (let i = 0)
}
Code: 10:11:15 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
}
}
// search for equi leaders
for (let i = 0; i <)
}
Code: 10:11:45 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === candidate) {
count++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i])
}
}
Code: 10:11:56 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
count++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] ===)
}
}
Code: 10:12:26 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
let r
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
}
}
}
Code: 10:12:57 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount)
}
}
Code: 10:13:15 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor())
}
}
Code: 10:13:26 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1)
}
}
Code: 10:13:50 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor(N - i))
}
}
Code: 10:14:03 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
}
}
}
Code: 10:14:16 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
const equiLeaders = [];
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
}
}
}
Code: 10:14:28 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
const equiLeaders = [];
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
equiLeaders.push(i);
}
}
return equiLeaders
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'object' found
Code: 10:14:42 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
const equiLeaders = [];
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
equiLeaders.push(i);
}
}
console.log(equiLeaders)
return equiLeaders
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'object' foundstdout:
[ 1 ]
Code: 10:14:51 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
const equiLeader = [];
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
equiLeaders.push(i);
}
}
console.log(equiLeaders)
return equiLeaders
}
Code: 10:15:04 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
const equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
TypeError: Assignment to constant variable. at solution (solution.js:38:32) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
Code: 10:15:11 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 1 expected 2
Code: 10:16:00 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i - 1) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Code: 10:16:01 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i - 1) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 3 expected 2
Code: 10:16:11 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Code: 10:16:43 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N -- i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Code: 10:16:49 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 3 expected 2
Code: 10:17:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
console.log(leftLeaderCount);
console.log(rightLeaderCount);
console.log()
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Code: 10:17:26 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
console.log(leftLeaderCount);
console.log(rightLeaderCount);
console.log(Math.floor(i / 2) + 1)
console.log()
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Code: 10:17:42 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
console.log('-----------')
console.log(leftLeaderCount);
console.log(Math.floor(i / 2) + 1)
console.log(rightLeaderCount);
console.log(Math.floor((N - 1 - i) / 2) + 1)
if (leftLeaderCount >= Math.floor(i / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
expand all
Example tests
1.
0.076 s
WRONG ANSWER,
got 3 expected 2
stdout:
----------- 1 1 3 3 ----------- 1 1 3 3 ----------- 2 2 2 2 ----------- 3 2 1 2 ----------- 4 3 0 1 ----------- 4 3 0 1
Code: 10:18:50 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
console.log('-----------')
console.log(leftLeaderCount);
console.log(Math.floor(i / 2) + 1)
console.log(rightLeaderCount);
console.log(Math.floor((N - 1 - i) / 2) + 1)
if (leftLeaderCount >= Math.floor(i + / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Code: 10:18:52 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
console.log('-----------')
console.log(leftLeaderCount);
console.log(Math.floor(i / 2) + 1)
console.log(rightLeaderCount);
console.log(Math.floor((N - 1 - i) / 2) + 1)
if (leftLeaderCount >= Math.floor(i + 1 / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
expand all
Example tests
1.
0.076 s
WRONG ANSWER,
got 1 expected 2
stdout:
----------- 1 1 3 3 ----------- 1 1 3 3 ----------- 2 2 2 2 ----------- 3 2 1 2 ----------- 4 3 0 1 ----------- 4 3 0 1
Code: 10:19:16 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
console.log('-----------')
console.log(leftLeaderCount);
console.log(Math.floor((i + 1) / 2) + 1)
console.log(rightLeaderCount);
console.log(Math.floor((N - 1 - i) / 2) + 1)
if (leftLeaderCount >= Math.floor(i + 1 / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Code: 10:19:19 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
console.log('-----------')
console.log(leftLeaderCount);
console.log(Math.floor((i + 1) / 2) + 1)
console.log(rightLeaderCount);
console.log(Math.floor((N - 1 - i) / 2) + 1)
if (leftLeaderCount >= Math.floor((i + 1) / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
----------- 1 1 3 3 ----------- 1 2 3 3 ----------- 2 2 2 2 ----------- 3 3 1 2 ----------- 4 3 0 1 ----------- 4 4 0 1
Code: 10:19:29 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor((i + 1) / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
Code: 10:19:35 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor((i + 1) / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis
Code: 10:19:38 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// find the leader of the orignal array
const N = A.length;
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0 || stack[stack.length - 1] === A[i]) {
stack.push(A[i]);
} else {
stack.pop();
}
}
const leader = stack.pop();
let totalLeaderCount = 0;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
totalLeaderCount++;
}
}
// search for equi leaders
let equiLeaderCount = 0;
let leftLeaderCount = 0;
let rightLeaderCount = totalLeaderCount;
for (let i = 0; i < N; i++) {
if (A[i] === leader) {
leftLeaderCount++;
rightLeaderCount--;
}
if (leftLeaderCount >= Math.floor((i + 1) / 2) + 1 &&
rightLeaderCount >= Math.floor((N - 1 - i) / 2) + 1) {
equiLeaderCount++;
}
}
return equiLeaderCount;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
1.
0.072 s
OK