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;
- import java.util.Arrays;
- public class Array<E> {
- private E arr[];
- private int n;
- public Array(int n) {
- this.arr = (E[]) new Object[n];
- this.n = n;
- }
- public Array(E[] arr, int n) {
- this.arr = arr;
- this.n = n;
- }
- public int getSize() {
- return n;
- }
- public void set(int pos, E elem) {
- if(pos < n&&pos >= 0)
- arr[pos] = elem;
- else System.out.println("Out of bounds");
- }
- public E get(int pos) {
- if(pos < n && pos >= 0)
- return arr[pos];
- else System.out.println("Out of bounds");
- return null;
- }
- public static int findClosest(double val, Array<Integer> niza) {
- double dist = Math.abs(niza.get(0) - val);
- int index = 0;
- for(int i=1; i<niza.getSize(); i++) {
- if(dist > Math.abs(niza.get(i) - val)) {
- dist = Math.abs(niza.get(i) - val);
- index = i;
- } else if(dist == Math.abs(niza.get(i) - val)) {
- if(niza.get(i) < niza.get(index)) {
- index = i;
- }
- }
- }
- return niza.get(index);
- }
- public static int brojDoProsek(Array<Integer> niza){
- int sum = 0;
- for(int i=0; i<niza.getSize(); i++)
- sum += niza.get(i);
- return findClosest((double)sum/niza.getSize(), niza);
- }
- 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