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 23 minutes
Notes
not defined yet
Task timeline
Code: 15:31:53 UTC,
java,
autosave
Code: 15:36:13 UTC,
java,
autosave
Code: 15:36:30 UTC,
java,
autosave
Code: 15:36:54 UTC,
java,
autosave
Code: 15:37:14 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = clone(A);
}
}
Code: 15:37:38 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone;
}
}
Code: 15:38:08 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int tmpLeader =
}
Code: 15:38:35 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int tmpLeader = copyA[A.length/2];
for(int i=0; i<tmpLeader; i++){
}
}
Code: 15:39:03 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int tmpLeader = copyA[A.length/2];
for(int i=0; i<tmpLeader; i++){
tmp[i] == tmpLeader
}
}
Code: 15:39:34 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(tmp[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>/2)
}
Code: 15:39:45 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(tmp[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
}
}
Code: 15:39:55 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
}
}
Code: 15:40:26 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;}
}
Code: 15:40:48 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length
}
Code: 15:41:18 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader
}
Code: 15:41:49 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
for(int current : A){
right ++;
left --;
if(current == leader)
}
}
Code: 15:42:00 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
}
}
}
Code: 15:42:25 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
}
}
}
Code: 15:42:46 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
}
Code: 15:48:45 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
r
}
Code: 15:48:54 UTC,
java,
verify,
result: Failed
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.008 s
WRONG ANSWER,
got -1 expected 2
Code: 15:49:05 UTC,
java,
verify,
result: Failed
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int right = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 0 expected 2
Code: 15:50: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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int n = 0;
int left = A.length;
int rightLeaderCnt = 0;
int leftLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
return answer;
}
}
Code: 15:50:42 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
right ++;
left --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
return answer;
}
}
Code: 15:50:53 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt ++;
leftLeaderCnt --;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
return answer;
}
}
Code: 15:51:05 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt == leftLeaderCnt){
answer ++;
}
}
return answer;
}
}
Code: 15:51:23 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 == leftLeaderCnt){
answer ++;
}
}
return answer;
}
}
Code: 15:51:37 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Code: 15:51:42 UTC,
java,
verify,
result: Failed
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -1;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 0 expected 2
Code: 15:52:33 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return -;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Code: 15:52:41 UTC,
java,
verify,
result: Failed
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(A);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return 0;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 0 expected 2
Code: 15:53:14 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(copyA);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return 0;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Code: 15:53:15 UTC,
java,
verify,
result: Failed
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(copyA);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<tmpLeader; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return 0;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 0 expected 2
Code: 15:53:47 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(copyA);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<ㅊ; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return 0;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Code: 15:53:55 UTC,
java,
verify,
result: Passed
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(copyA);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<copyA.length; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return 0;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Analysis
Code: 15:54:02 UTC,
java,
verify,
result: Passed
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(copyA);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<copyA.length; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return 0;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Analysis
Code: 15:54:06 UTC,
java,
final,
score: 
100
// 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");
import java.util.*;
class Solution {
public int solution(int[] A) {
//리더찾기
int[] copyA = new int[A.length];
copyA = A.clone();
Arrays.sort(copyA);
int leader = 0;
int tmpLeader = copyA[A.length/2];
int cntLeader = 0;
for(int i=0; i<copyA.length; i++){
if(copyA[i] == tmpLeader){
cntLeader ++;
}
}
if(cntLeader>=A.length/2){
leader = tmpLeader;
}
else{
return 0;
}
int left = 0;
int right = A.length;
int leftLeaderCnt = 0;
int rightLeaderCnt = cntLeader;
int answer = 0;
for(int current : A){
left ++;
right --;
if(current == leader){
rightLeaderCnt --;
leftLeaderCnt ++;
}
if(rightLeaderCnt > right/2 && leftLeaderCnt > left/2){
answer ++;
}
}
return answer;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.008 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.008 s
OK
1.
0.004 s
OK