Source : https://www.hackerrank.com/challenges/s10-interquartile-range
Objective
In this challenge, we practice calculating the interquartile range. We recommend you complete the Quartiles challenge before attempting this problem.
Task
The interquartile range of an array is the difference between its first () and third () quartiles (i.e., ).
Given an array, , of integers and an array, , representing the respective frequencies of 's elements, construct a data set, , where each occurs at frequency . Then calculate and print 's interquartile range, rounded to a scale of decimal place (i.e., format).
Tip: Be careful to not use integer division when averaging the middle two elements for a data set with an even number of elements, and be sure to not include the median in your upper and lower data sets.
Input Format
The first line contains an integer, , denoting the number of elements in arrays and .
The second line contains space-separated integers describing the respective elements of array .
The third line contains space-separated integers describing the respective elements of array .
Constraints
- , where is the element of array .
- , where is the element of array .
- The number of elements in is equal to .
Output Format
Print the interquartile range for the expanded data set on a new line. Round your answer to a scale of decimal place (i.e., format).
Sample Input
66 12 8 10 20 165 4 3 2 1 5
Sample Output
9.0
Explanation
The given data is:
First, we create data set containing the data from set at the respective frequencies specified by :
As there are an even number of data points in the original ordered data set, we will split this data set exactly in half:
Lower half (L): 6, 6, 6, 6, 6, 8, 8, 8, 10, 10
Upper half (U): 12, 12, 12, 12, 16, 16, 16, 16, 16, 20
Next, we find . There are elements in half, so is the average of the middle two elements: and . Thus, .
Next, we find .There are elements in half, so is the average of the middle two elements: and . Thus, .
From this, we calculate the interquartile range as and print as our answer.
Source : https://www.hackerrank.com/challenges/s10-interquartile-range
Solution
// github.com/RodneyShag | |
import java.util.Scanner; | |
import java.util.Arrays; | |
public class Solution { | |
public static void main(String[] args) { | |
/* Save input */ | |
Scanner scan = new Scanner(System.in); | |
int size = scan.nextInt(); | |
int [] element = new int[size]; | |
int [] frequency = new int[size]; | |
for (int i = 0; i < size; i++) { | |
element[i] = scan.nextInt(); | |
} | |
int numElements = 0; | |
for (int i = 0; i < size; i++) { | |
frequency[i] = scan.nextInt(); | |
numElements += frequency[i]; | |
} | |
scan.close(); | |
/* Create and sort our data set */ | |
int [] data = new int[numElements]; | |
int dataIndex = 0; | |
for (int i = 0; i < size; i++) { | |
for (int j = 0; j < frequency[i]; j++) { | |
data[dataIndex] = element[i]; | |
dataIndex++; | |
} | |
} | |
Arrays.sort(data); | |
/* Calculate interquartile range */ | |
double q1 = findMedian(data, 0, data.length / 2 - 1); | |
double q3 = findMedian(data, (data.length + 1) / 2, data.length - 1); | |
System.out.println(q3 - q1); | |
} | |
/* Treats elements from "start" to "end" (inclusive) as an array and calculates its median */ | |
private static double findMedian(int [] array, int start, int end) { | |
if ((end - start) % 2 == 0) { // odd number of elements | |
return (array[(end + start) / 2]); | |
} else { // even number of elements | |
int value1 = array[(end + start) / 2]; | |
int value2 = array[(end + start) / 2 + 1]; | |
return (value1 + value2) / 2.0; | |
} | |
} | |
} |
No comments:
Post a Comment