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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 3 minutes
Notes
not defined yet
Code: 07:22:23 UTC,
java,
autosave
Code: 07:22:30 UTC,
java,
autosave
// 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
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int leaderCnt = 0; // 기준이 되는 leader 갯수
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
leaderCnt = entry.getValue();
}
}
int equiLeaderCnt = 0;
int LeftLeaderCnt = leaderCnt;
int RightLeaderCnt = 0; // 리더 숫자의 갯수를 넣어줌
for (int i=A.length-1; i>=0; i--) {
if (A[i] == leaderValue) {
LeftLeaderCnt--;
RightLeaderCnt++;
}
if (LeftLeaderCnt > i/2 && RightLeaderCnt > ((A.length-1)/2)) {
equiLeaderCnt++;
}
}
return equiLeaderCnt;
}
}
Code: 07:22:33 UTC,
java,
verify,
result: Failed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// 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
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int leaderCnt = 0; // 기준이 되는 leader 갯수
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
leaderCnt = entry.getValue();
}
}
int equiLeaderCnt = 0;
int LeftLeaderCnt = leaderCnt;
int RightLeaderCnt = 0; // 리더 숫자의 갯수를 넣어줌
for (int i=A.length-1; i>=0; i--) {
if (A[i] == leaderValue) {
LeftLeaderCnt--;
RightLeaderCnt++;
}
if (LeftLeaderCnt > i/2 && RightLeaderCnt > ((A.length-1)/2)) {
equiLeaderCnt++;
}
}
return equiLeaderCnt;
}
}
Analysis
expand all
Example tests
1.
0.008 s
WRONG ANSWER,
got 1 expected 2
Code: 07:25:01 UTC,
java,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// 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
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int leaderCnt = 0; // 기준이 되는 leader 갯수
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
leaderCnt = entry.getValue();
}
}
int equiLeaderCnt = 0;
int LeftLeaderCnt = leaderCnt;
int RightLeaderCnt = 0; // 리더 숫자의 갯수를 넣어줌
for (int i=A.length-1; i>=0; i--) {
if (A[i] == leaderValue) {
LeftLeaderCnt--;
RightLeaderCnt++;
}
if (LeftLeaderCnt > i/2 && RightLeaderCnt > ((A.length-ㅑ)/2)) {
equiLeaderCnt++;
}
}
return equiLeaderCnt;
}
}
Code: 07:25:11 UTC,
java,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// 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
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int leaderCnt = 0; // 기준이 되는 leader 갯수
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
leaderCnt = entry.getValue();
}
}
int equiLeaderCnt = 0;
int LeftLeaderCnt = leaderCnt;
int RightLeaderCnt = 0; // 리더 숫자의 갯수를 넣어줌
for (int i=A.length-1; i>=0; i--) {
if (A[i] == leaderValue) {
LeftLeaderCnt--;
RightLeaderCnt++;
}
if (LeftLeaderCnt > i/2 && RightLeaderCnt > ((A.length-i)/2)) {
equiLeaderCnt++;
}
}
return equiLeaderCnt;
}
}
Code: 07:25:13 UTC,
java,
verify,
result: Passed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// 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
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int leaderCnt = 0; // 기준이 되는 leader 갯수
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
leaderCnt = entry.getValue();
}
}
int equiLeaderCnt = 0;
int LeftLeaderCnt = leaderCnt;
int RightLeaderCnt = 0; // 리더 숫자의 갯수를 넣어줌
for (int i=A.length-1; i>=0; i--) {
if (A[i] == leaderValue) {
LeftLeaderCnt--;
RightLeaderCnt++;
}
if (LeftLeaderCnt > i/2 && RightLeaderCnt > ((A.length-i)/2)) {
equiLeaderCnt++;
}
}
return equiLeaderCnt;
}
}
Analysis
Code: 07:25:17 UTC,
java,
final,
score: 
100
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// 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
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int leaderCnt = 0; // 기준이 되는 leader 갯수
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
leaderCnt = entry.getValue();
}
}
int equiLeaderCnt = 0;
int LeftLeaderCnt = leaderCnt;
int RightLeaderCnt = 0; // 리더 숫자의 갯수를 넣어줌
for (int i=A.length-1; i>=0; i--) {
if (A[i] == leaderValue) {
LeftLeaderCnt--;
RightLeaderCnt++;
}
if (LeftLeaderCnt > i/2 && RightLeaderCnt > ((A.length-i)/2)) {
equiLeaderCnt++;
}
}
return equiLeaderCnt;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.008 s
OK
2.
0.008 s
OK
3.
0.004 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
3.
0.008 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.008 s
OK
1.
0.008 s
OK