Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Array<E> {
- private E data[];
- private int size;
- public Array(int size) {
- data = (E[])new Object[size];
- this.size = size;
- }
- public int getLength() {
- return size;
- }
- public E get(int position) {
- if(position >= 0 && position < size)
- return data[position];
- return null;
- }
- public void set(int position, E o) {
- if(position >= 0 && position < size)
- data[position] = o;
- }
- public static int brojDoProsek(Array<Integer> niza){
- int sum = 0, avg = 0;
- for(int i = 0; i < niza.getLength(); i++) {
- sum += niza.get(i);
- }
- avg = sum / niza.getLength();
- int min = Math.abs(avg - niza.get(0)), index = 0;
- for(int i = 1; i < niza.getLength(); i++) {
- if(Math.abs(avg - niza.get(i)) < min) {
- min = Math.abs(avg - niza.get(i));
- index = i;
- }
- if(Math.abs(avg - niza.get(i)) == min) {
- if(niza.get(i) < niza.get(index)) {
- min = niza.get(i);
- index = i;
- }
- }
- }
- return niza.get(index);
- }
- public static void main(String[] args) throws IOException{
- BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- Array<Integer> niza = new Array<Integer>(N);
- for(int i = 0; i < N; i++) {
- s = stdin.readLine();
- niza.set(i, Integer.parseInt(s));
- }
- System.out.println(brojDoProsek(niza));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement