思路:
先排序,两个count变量记录。有点暴力
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
int mid = (n-1)/2;
int count1 = 0;
int count2 = 0;
for(int j=0;j<mid;j++) {
if(a[j] < a[mid]) {
count1++;
}
}
for(int k=mid+1;k<n;k++) {
if(a[k] > a[mid]) {
count2++;
}
}
if(count1 == count2) {
System.out.println(a[mid]);
}else {
System.out.println(-1);
}
}
}