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 9 minutes
Notes
not defined yet
Task timeline
Code: 06:34:59 UTC,
java,
autosave
Code: 06:35: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.Map;
import java.util.HashMap;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A)
}
}
Code: 06:36: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.Map;
import java.util.HashMap;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.)
}
}
Code: 06:36:31 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.Map;
import java.util.HashMap;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer
}
}
Code: 06:36: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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
}
}
Code: 06:37:19 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer
}
Code: 06:37: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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
}
}
Code: 06:37:44 UTC,
java,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
// 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
}
Code: 06:38:01 UTC,
java,
autosave
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
// 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
priv
}
Code: 06:39:16 UTC,
java,
autosave
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
// 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private
}
Code: 06:39:30 UTC,
java,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
// 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int findLeader(int[] A) {
}
}
Code: 06:39:51 UTC,
java,
autosave
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
// 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int findLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:40:06 UTC,
java,
autosave
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
// 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader =
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int findLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:40:17 UTC,
java,
autosave
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
// 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int key = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
key = entry.getKey();
max = entry.getValue();
}
}
Vector<Integer> record = new Vector<>();
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:40: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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
fo
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:41: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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
}
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:41: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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
i
}
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:42:02 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
int equiTwo = ((A.length - (idx + 1)))
}
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:42: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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
int equiTwo = ((A.length - (idx + 1)) / 2) + 1;
if ((left >= equiOne) && (right >= equiTwo)) {
result++;
}
}
return result;
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:42:34 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
int equiTwo = ((A.length - (idx + 1)) / 2) + 1;
if ((left >= equiOne) && (right >= equiTwo)) {
result++;
}
}
return result;
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == key) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Analysis
Compile error
Solution.java:31: error: cannot find symbol if (num == key) { ^ symbol: variable key location: class Solution 1 error
Code: 06:42:50 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
int equiTwo = ((A.length - (idx + 1)) / 2) + 1;
if ((left >= equiOne) && (right >= equiTwo)) {
result++;
}
}
return result;
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == leader) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Analysis
Code: 06:42:50 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
int equiTwo = ((A.length - (idx + 1)) / 2) + 1;
if ((left >= equiOne) && (right >= equiTwo)) {
result++;
}
}
return result;
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == leader) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Code: 06:42:57 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
int equiTwo = ((A.length - (idx + 1)) / 2) + 1;
if ((left >= equiOne) && (right >= equiTwo)) {
result++;
}
}
return result;
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == leader) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Analysis
Code: 06:43:02 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.Map;
import java.util.HashMap;
import java.util.Vector;
class Solution {
public int solution(int[] A) {
Vector<Integer> record = getRecord(A);
int result = 0;
for (int idx = 0; idx < A.length; idx++) {
int left = record.elementAt(idx);
int right = record.lastElement() - left;
int equiOne = ((idx + 1) / 2) + 1;
int equiTwo = ((A.length - (idx + 1)) / 2) + 1;
if ((left >= equiOne) && (right >= equiTwo)) {
result++;
}
}
return result;
}
private Vector<Integer> getRecord(int[] A) {
Vector<Integer> record = new Vector<>();
int leader = getLeader(A);
int count = 0;
for (int num : A) {
if (num == leader) {
count++;
}
record.add(count);
}
return record;
}
private int getLeader(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : A) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int leader = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
leader = entry.getKey();
max = entry.getValue();
}
}
return leader;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.012 s
OK
2.
0.012 s
OK
3.
0.012 s
OK
1.
0.008 s
OK
2.
0.012 s
OK
3.
0.012 s
OK
1.
0.012 s
OK
2.
0.012 s
OK
1.
0.012 s
OK
1.
0.012 s
OK