Advertisement
bkit4s0

Untitled

Apr 8th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class type {
  6.     static int getMaxOfArray(int a[]) {
  7.         int max = 0;
  8.         for (int i = 0; i < a.length; i++) {
  9.             if (a[i] > max)
  10.                 max = a[i];
  11.         }
  12.         return max;
  13.     }
  14.  
  15.     public static void main(String args[]) throws NumberFormatException,
  16.             IOException {
  17.  
  18.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  19.         int t = Integer.parseInt(in.readLine());
  20.         while (t-- > 0) {
  21.             int a[], b[], result, max;
  22.             int n = Integer.parseInt(in.readLine());
  23.  
  24.             /* define array b to update result */
  25.             a = new int[n];
  26.             String[] num = new String[n];
  27.             num = in.readLine().split(" ");
  28.             for (int i = 0; i < n; i++) {
  29.                 a[i] = Integer.parseInt(num[i]);
  30.             }
  31.             b = new int[getMaxOfArray(a)];
  32.            
  33.             /* update result */
  34.             for (int i = 0; i < n; i++) {
  35.                 b[a[i]-1]++;
  36.             }
  37.             max = 0;
  38.             result = 0;
  39.            
  40.             /* find max of array b and index of max*/
  41.             for (int i = 0; i < b.length; i++) {
  42.                 if (b[i] == max) {
  43.                     if (i+1 < result)
  44.                         result = i+1;
  45.                 } else if (b[i] > max) {
  46.                     max = b[i];
  47.                     result = i+1;
  48.                 }
  49.             }
  50.            
  51.             /*print result*/
  52.             System.out.println(result + " " + b[result-1]);
  53.         }
  54.  
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement