-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuartiles.java
43 lines (36 loc) · 1.28 KB
/
Quartiles.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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
double q1,median,q3;
if( (n%2) != 0 ){
median = arr[n/2];
} else {
median = ((double)arr[n/2] + (double)arr[(n/2) - 1]) / 2.0;
}
if( ((n/2)%2) != 0 ){
q1 = arr [ ((n/2)/2) ];
q3 = arr [ (n/2) + ((n/2)/2) ];
} else {
q1 = ((double)arr[((n/2)/2)] + (double)arr[((n/2)/2) - 1]) / 2.0;
if( (n%2) == 0 )
q3 = ((double)arr[(n/2) + ((n/2)/2)] + (double)arr[(n/2) + ((n/2)/2) - 1]) / 2.0;
else
q3 = ((double)arr[(n/2) + ((n/2)/2)] + (double)arr[(n/2) + ((n/2)/2) + 1]) / 2.0;
}
System.out.println((int)q1);
System.out.println((int)median);
System.out.println((int)q3);
}
}