fork download
  1. // print() -> p();
  2. // println() -> pn();
  3. // next() -> ns();
  4. // nextInt() -> ni();
  5. // nextLong() -> nl();
  6. import java.io.*;
  7. import java.util.*;
  8.  
  9. public class Main {
  10.  
  11. static void solve() {
  12. int n = ni();
  13.  
  14. // if(n <= 2) {
  15. // pn("NO");
  16. // return;
  17. // }
  18.  
  19. int[] f = new int[100001];
  20.  
  21. int max1 = -1, max2 = -1;
  22. for(int i = 0; i < n; i++) {
  23. int val = ni();
  24. f[val]++;
  25.  
  26. if(max1 < val) {
  27. max2 = max1;
  28. max1 = val;
  29. }
  30.  
  31. else if(max2 < val) {
  32. max2 = val;
  33. }
  34. }
  35.  
  36. if(max1 != max2){
  37. if(f[max1] + f[max2] < n) pn("YES");
  38. else pn("NO");
  39. }
  40.  
  41. else{
  42. if(2 * f[max1] <= n + 1) pn("YES");
  43. else pn("NO");
  44. }
  45. }
  46.  
  47. public static void main(String[] args) throws Exception {
  48. int T = ni();
  49.  
  50. while (T-- > 0) {
  51. solve();
  52. }
  53.  
  54. out.flush();
  55. out.close();
  56. }
  57.  
  58. // gcd
  59. static long gcd(long a, long b) {
  60. return (b == 0) ? a : gcd(b, a % b);
  61. }
  62.  
  63. // modular a^b
  64. static long modPow(long a, long b, long mod) {
  65. long res = 1;
  66. while (b > 0) {
  67. if ((b & 1) == 1) res = (res * a) % mod;
  68. a = (a * a) % mod;
  69. b >>= 1;
  70. }
  71. return res;
  72. }
  73.  
  74. // global io
  75. static FastReader in = new FastReader();
  76. static PrintWriter out = new PrintWriter(System.out);
  77.  
  78. // mod = (int)1e9+ 7
  79. static final int MOD = 1_000_000_007;
  80.  
  81. // short input methods
  82. static String ns() {
  83. return in.next();
  84. }
  85. static int ni() {
  86. return in.nextInt();
  87. }
  88. static long nl() {
  89. return in.nextLong();
  90. }
  91.  
  92. // short output methods
  93. static void p(Object o) {
  94. out.print(o);
  95. }
  96. static void pn(Object o) {
  97. out.println(o);
  98. }
  99.  
  100. // fast reader
  101. static class FastReader {
  102.  
  103. FastReader() {
  104. }
  105.  
  106. String next() {
  107. while (st == null || !st.hasMoreTokens()) {
  108. try {
  109. st = new StringTokenizer(br.readLine());
  110. } catch (IOException e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. return st.nextToken();
  115. }
  116.  
  117. int nextInt() {
  118. return Integer.parseInt(next());
  119. }
  120. long nextLong() {
  121. return Long.parseLong(next());
  122. }
  123. }
  124. }
Runtime error #stdin #stdout #stderr 0.09s 52768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.StringTokenizer.<init>(StringTokenizer.java:199)
	at java.base/java.util.StringTokenizer.<init>(StringTokenizer.java:236)
	at Main$FastReader.next(Main.java:112)
	at Main$FastReader.nextInt(Main.java:121)
	at Main.ni(Main.java:86)
	at Main.main(Main.java:48)