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 Java 8
Time spent on task 52 minutes
Notes
not defined yet
Task timeline
Code: 09:44:32 UTC,
java,
autosave
Code: 10:02:28 UTC,
java,
autosave
Code: 10:02:39 UTC,
java,
autosave
Code: 10:35:24 UTC,
java,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Stack<Integer> equiLeaderStack = new Stack<Integer>();
int dominatorOccur=0;
for(int i=0; i<A.length; i++) {
if (equiLeaderStack.isEmpty()) {
equiLeaderStack.push(A[i]);
continue;
}
if (equiLeaderStack.peek() == A[i]) {
equiLeaderStack.push(A[i]);
} else {
equiLeaderStack.pop();
}
}
if(equiLeaderStack.isEmpty()) {
return 0;
}
for(int i=0; i<A.length; i++) {
if(equiLeaderStack.peek()==A[i]){
dominatorOccur++;
}
}
if (dominatorOccur<=A.length/2) {
return 0;
}
int dominator = equiLeaderStack.peek();
int equiCount = 0;
int dominatorCnt = 0;
boolean leftCondition, rightCondition;
for(int i=0;i<A.length;i++) {
if (A[i] == dominator) {
dominatorCnt++; //i번째 까지의 dominatorCnt 체크
}
//둘로 나눴을 때 왼쪽 오른쪽 둘 다 만족하는지 체크
leftCondition = dominatorCnt>(i+1)/2;
rightCondition = (dominatorOccur-dominatorCnt)>(A.length-i-1)/2;
if( leftCondition && rightCondition ) {
equiCount++;
}
}
return equiCount;
}
}
Code: 10:35:31 UTC,
java,
verify,
result: Passed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
// you can also use imports, for example:
// import java.util.*;
import java.util.Stack;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Stack<Integer> equiLeaderStack = new Stack<Integer>();
int dominatorOccur=0;
for(int i=0; i<A.length; i++) {
if (equiLeaderStack.isEmpty()) {
equiLeaderStack.push(A[i]);
continue;
}
if (equiLeaderStack.peek() == A[i]) {
equiLeaderStack.push(A[i]);
} else {
equiLeaderStack.pop();
}
}
if(equiLeaderStack.isEmpty()) {
return 0;
}
for(int i=0; i<A.length; i++) {
if(equiLeaderStack.peek()==A[i]){
dominatorOccur++;
}
}
if (dominatorOccur<=A.length/2) {
return 0;
}
int dominator = equiLeaderStack.peek();
int equiCount = 0;
int dominatorCnt = 0;
boolean leftCondition, rightCondition;
for(int i=0;i<A.length;i++) {
if (A[i] == dominator) {
dominatorCnt++; //i번째 까지의 dominatorCnt 체크
}
//둘로 나눴을 때 왼쪽 오른쪽 둘 다 만족하는지 체크
leftCondition = dominatorCnt>(i+1)/2;
rightCondition = (dominatorOccur-dominatorCnt)>(A.length-i-1)/2;
if( leftCondition && rightCondition ) {
equiCount++;
}
}
return equiCount;
}
}
Analysis
Code: 10:35:39 UTC,
java,
verify,
result: Passed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
// you can also use imports, for example:
// import java.util.*;
import java.util.Stack;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Stack<Integer> equiLeaderStack = new Stack<Integer>();
int dominatorOccur=0;
for(int i=0; i<A.length; i++) {
if (equiLeaderStack.isEmpty()) {
equiLeaderStack.push(A[i]);
continue;
}
if (equiLeaderStack.peek() == A[i]) {
equiLeaderStack.push(A[i]);
} else {
equiLeaderStack.pop();
}
}
if(equiLeaderStack.isEmpty()) {
return 0;
}
for(int i=0; i<A.length; i++) {
if(equiLeaderStack.peek()==A[i]){
dominatorOccur++;
}
}
if (dominatorOccur<=A.length/2) {
return 0;
}
int dominator = equiLeaderStack.peek();
int equiCount = 0;
int dominatorCnt = 0;
boolean leftCondition, rightCondition;
for(int i=0;i<A.length;i++) {
if (A[i] == dominator) {
dominatorCnt++; //i번째 까지의 dominatorCnt 체크
}
//둘로 나눴을 때 왼쪽 오른쪽 둘 다 만족하는지 체크
leftCondition = dominatorCnt>(i+1)/2;
rightCondition = (dominatorOccur-dominatorCnt)>(A.length-i-1)/2;
if( leftCondition && rightCondition ) {
equiCount++;
}
}
return equiCount;
}
}
Analysis
Code: 10:35:43 UTC,
java,
final,
score: 
100
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
// you can also use imports, for example:
// import java.util.*;
import java.util.Stack;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Stack<Integer> equiLeaderStack = new Stack<Integer>();
int dominatorOccur=0;
for(int i=0; i<A.length; i++) {
if (equiLeaderStack.isEmpty()) {
equiLeaderStack.push(A[i]);
continue;
}
if (equiLeaderStack.peek() == A[i]) {
equiLeaderStack.push(A[i]);
} else {
equiLeaderStack.pop();
}
}
if(equiLeaderStack.isEmpty()) {
return 0;
}
for(int i=0; i<A.length; i++) {
if(equiLeaderStack.peek()==A[i]){
dominatorOccur++;
}
}
if (dominatorOccur<=A.length/2) {
return 0;
}
int dominator = equiLeaderStack.peek();
int equiCount = 0;
int dominatorCnt = 0;
boolean leftCondition, rightCondition;
for(int i=0;i<A.length;i++) {
if (A[i] == dominator) {
dominatorCnt++; //i번째 까지의 dominatorCnt 체크
}
//둘로 나눴을 때 왼쪽 오른쪽 둘 다 만족하는지 체크
leftCondition = dominatorCnt>(i+1)/2;
rightCondition = (dominatorOccur-dominatorCnt)>(A.length-i-1)/2;
if( leftCondition && rightCondition ) {
equiCount++;
}
}
return equiCount;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.008 s
OK
1.
0.004 s
OK
2.
0.008 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
1.
0.004 s
OK
1.
0.008 s
OK