-
Notifications
You must be signed in to change notification settings - Fork 0
/
P16.java
47 lines (45 loc) · 1.28 KB
/
P16.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.*;
import java.util.*;
import java.util.ArrayList;
public class Solution {
public static ArrayList<Integer> majorityElementII(ArrayList<Integer> arr) {
// Moore's Voting Algo
// as count>N/3 required at max 2 candidates possible
int n = arr.size();
int count1 = 0;
int candidate1 = -1;
int count2 = 0;
int candidate2 = -1;
for (int i = 0; i < n; i++) {
int temp = arr.get(i);
if (temp == candidate1)
count1++;
else if (temp == candidate2)
count2++;
else if (count1 == 0) {
candidate1 = temp;
count1++;
} else if (count2 == 0) {
candidate2 = temp;
count2++;
} else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
for (int a : arr) {
if (a == candidate1)
count1++;
else if (a == candidate2)
count2++;
}
ArrayList<Integer> res = new ArrayList<Integer>();
if (count1 > (n / 3))
res.add(candidate1);
if (count2 > (n / 3))
res.add(candidate2);
return res;
}
}