(SPOJ) Notas - Solution

Link to the problem: http://br.spoj.com/problems/NOTAS14/

The solution below check what is the most frequent grade. Then, we go through the array of grades and get the last one that has the frequency equals to the maximum frequency.


import java.io.*;
import java.util.*;

class Main {
    public static int reader(BufferedReader br) throws NumberFormatException, IOException {     
        int n;
        int resp = 0;     
      
        while (true) {         
            n = br.read();         
            if (n >= '0' && n <= '9') {
                break;
            }
        }
           
        while (true) {         
            resp = resp*10 + n-'0';         
            n = br.read();         
            if (n < '0' || n > '9') {
                break;     
            }
        }
      
        return resp;     
    }
   
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        int numStudents = reader(br);

        int[] grades = new int[101];
       
        int maxFreq = 0;
        for (int i = 0; i < numStudents; i++) {
            int n = reader(br);
            grades[n]++;
            maxFreq = Math.max(maxFreq, grades[n]);
        }
       
        int maxGrade = 0;
        for (int i = 0; i <= 100; i++) {
            if (grades[i] == maxFreq) {
                maxGrade = Math.max(maxGrade, i);
            }
        }
       
        bw.write(maxGrade+"\n");
       
        bw.flush();
        bw.close();
                          
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução