-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
65 lines (48 loc) · 1.6 KB
/
Solution.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int l = 0;
int r = n-1;
int[] lookup = new int[n+1];
Arrays.fill(lookup,-1);
int val = 0;
val = majorityCount(n, arr, l, r, lookup);
if(val == 0){
System.out.println("No");
}else{
System.out.println("Yes");
}
}
public static int majorityCount(int n, int arr[], int l, int r, int[] lookup){
int m = (l+(r-1))/2;
if(l==r){
return arr[l];
}else{
int x = majorityCount(n, arr, l, m,lookup);
int y = majorityCount(n, arr, m+1, r,lookup);
// System.out.println(x+":"+y);
if((x == 0) && (y == 0)){
return 0;
}else if(x == 0 && y !=0){
return y;
}else if(x != 0 && y ==0){
return x;
}else if(x != y){
return 0;
}else{
return x;
}
}
}
}