fork(2) download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. int t = sc.nextInt();
  6. for (; 0 < t; t--) {
  7. int n = sc.nextInt();
  8. int[] tab = new int[n];
  9. for (int i = 0; i < tab.length; i++) {
  10. tab[i] = sc.nextInt();
  11. }
  12. srednia(tab);
  13. }
  14. }
  15.  
  16. private static void srednia(int[] tab) {
  17. int suma = 0;
  18. int srednia;
  19. int sredniaRszta = 0;
  20. int index = 0;
  21.  
  22. for (int value : tab) {
  23. suma += value;
  24. }
  25.  
  26. if (suma % tab.length != 0 && suma % tab.length >= tab.length / 2) {
  27. sredniaRszta = (suma / tab.length) + 1;
  28. srednia = suma / tab.length;
  29. } else {
  30. srednia = suma / tab.length;
  31. }
  32.  
  33. int[] nowaTablica = new int[tab.length];
  34. int najmniejszyElement = tab[0] - srednia;
  35. for (int i = 0; i < tab.length; i++) {
  36. nowaTablica[i] = tab[i] - srednia;
  37. if (nowaTablica[i] == 0) {
  38. index = i;
  39. break;
  40. } else if (sredniaRszta != 0 && nowaTablica[i] == 1) {
  41. index = i;
  42. break;
  43. } else {
  44. if (Math.abs(najmniejszyElement) > Math.abs(nowaTablica[i])) {
  45. najmniejszyElement = nowaTablica[i];
  46. index = i;
  47. }
  48. }
  49. }
  50.  
  51. System.out.println(tab[index]);
  52. }
  53.  
  54. private static Scanner sc = new Scanner(System.in);
  55. }
  56.  
Success #stdin #stdout 0.07s 2184192KB
stdin
10
5 1 2 3 4 5
6 1 2 3 4 3 1
7 3 1 2 4 5 6 4
7 4 1 2 3 5 6 4
6 1 2 3 4 5 6
6 3 4 1 2 5 6
4 5 16 12 7
4 3 16 14 7
5 4 3 1 0 0
5 4 3 0 0 1
stdout
3
2
3
4
3
3
12
7
1
1