-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathSolution.java
55 lines (48 loc) · 1.58 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// github.com/RodneyShag
import java.util.Scanner;
import java.util.Arrays;
import java.util.HashMap;
// Time Complexity: O(n log n) due to sorting
public class Solution {
public static void main(String[] args) {
/* Save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int [] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scan.nextInt();
}
scan.close();
/* Sort array: O(n log n) runtime */
Arrays.sort(array);
/* Calculate Mean */
int total = 0;
for (int num : array) {
total += num;
}
double mean = (double) total / size;
/* Calculate Median */
double median;
if (size % 2 == 0) {
median = (array[size / 2 - 1] + array[size / 2]) / 2.0;
} else {
median = array[size / 2];
}
/* Calculate Mode - if there's a tie, choose the smaller number */
HashMap<Integer, Integer> map = new HashMap();
int maxOccurrences = 0;
int mode = Integer.MAX_VALUE;
for (int num : array) {
map.merge(num, 1, Integer::sum);
int occurrences = map.get(num);
if (occurrences > maxOccurrences || (occurrences == maxOccurrences && num < mode)) {
maxOccurrences = occurrences;
mode = num;
}
}
/* Print results */
System.out.println(mean);
System.out.println(median);
System.out.println(mode);
}
}