# HackerRank Day 1: Interquartile Range | 10 Days of Statistics problem solution

In this Hackerrank Day 1: Interquartile Range 10 Days of Statistics problem we have Given an array of integers and an array representing the respective frequencies of the first array's elements, construct a data set where each value occurs at a frequency given in array second. Then calculate and print the array's interquartile range, rounded to a scale of 1 decimal place.

## Problem solution in Python programming.

```# Enter your code here. Read input from STDIN. Print output to STDOUT
import statistics as st

n = int(input())
data = list(map(int, input().split()))
freq = list(map(int, input().split()))

s = []
for i in range(n):
s += [data[i]] * freq[i]
N = sum(freq)
s.sort()

if n%2 != 0:
q1 = st.median(s[:N//2])
q3 = st.median(s[N//2+1:])
else:
q1 = st.median(s[:N//2])
q3 = st.median(s[N//2:])

ir = round(float(q3-q1), 1)
print(ir)```

## Problem solution in Java Programming.

```import java.io.*;
import java.util.*;

public class Solution {

private static double computeMean(int[] x) {
if (x.length % 2 == 0) {
return (x[x.length / 2] + x[(x.length / 2) - 1] + 0.0) / 2;
}
else {
return x[x.length / 2] + 0.0;
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();

int[] x = new int[n];
int[] f = new int[n];

for (int i = 0; i < n; ++i) {
x[i] = scanner.nextInt();
}

int l = 0;
for (int i = 0; i < n; ++i) {
f[i] = scanner.nextInt();
l += f[i];
}

int[] s = new int[l];
int k = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < f[i]; ++j, ++k) {
s[k] = x[i];
}
}

Arrays.sort(s);
double Q1 = 0, Q3 = 0;

if (l % 2 == 0) {
Q1 = computeMean(Arrays.copyOfRange(s, 0, l / 2));
Q3 = computeMean(Arrays.copyOfRange(s, l / 2, l));
}
else {
Q1 = computeMean(Arrays.copyOfRange(s, 0, l / 2));
Q3 = computeMean(Arrays.copyOfRange(s, (l / 2) + 1, l));
}

System.out.printf("%.1f\n", (Q3 - Q1));
}
}```

## Problem solution in C++ programming.

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

float median(vector<int>& arr, int start, int end, int &medianL, int &medianR) {
if (start > end)
return -1.0;
int length = end - start + 1;
if (length % 2 == 0) {
int tL = start + length / 2 - 1;
int tR = tL + 1;
float median = (arr[tL] + arr[tR]) / 2.f;

int i = tL;
while (arr[i] >= median && i > 0)
i--;
medianL = i;
i = tR;
while (arr[i] <= median && i < end)
i++;
medianR = i;

return median;
}
else {
int tL = start + length / 2 - 1;
int tR = tL + 2;
float median = arr[tL + 1];

int i = tL;
while (arr[i] >= median && i > 0)
i--;
medianL = i;
i = tR;
while (arr[i] <= median && i < end)
i++;
medianR = i;

return median;
}
}

int main() {
int N;
cin >> N;
vector<int> X(N);
vector<int> F(N);
for (int i = 0; i<N; i++) {
cin >> X[i];
}
int f = 0;
for (int i = 0; i<N; i++) {
cin >> F[i];
f += F[i];
}

vector<int> arr;
for (int i = 0; i<N; i++) {
for (int j = 0; j<F[i]; j++)
arr.push_back(X[i]);
}
sort(arr.begin(), arr.end());

int t1, t2, left, right;
float Q2 = median(arr, 0, f - 1, left, right);
float Q1 = median(arr, 0, left, t1, t2);
float Q3 = median(arr, right, f - 1, t1, t2);

float R = Q3 - Q1;
cout << fixed << setprecision(1) << R;
return 0;
}```

## Problem solution in C programming.

```#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct data{
int element;
int frequency;
};

double findvalue(struct data*,int,bool);

int compare(const void* a, const void* b){
return ((struct data*)a)->element - ((struct data*)b)->element;
}

int main(){
int n;
scanf("%d", &n);
struct data datas[n];
int i;
for(i = 0; i<n; i++){
scanf("%d", &(datas[i].element));
}
for(i = 0; i<n; i++){
scanf("%d", &(datas[i].frequency));
}
qsort(datas, n, sizeof(struct data), compare);
double q1 = 0.0,q3 = 0.0;
int sum = 0;
for(i=0; i<n; i++){
sum += datas[i].frequency;
}
switch(sum%4){
case 0:
q1 = findvalue(datas,sum/4,false);
q3 = findvalue(datas,sum*3/4,false);
break;
case 1:
q1 = findvalue(datas,sum/4,false);
q3 = findvalue(datas,sum*3/4+1,false);
break;
case 2:
q1 = findvalue(datas,sum/4+1,true);
q3 = findvalue(datas,sum*3/4+1,true);
break;
case 3:
q1 = findvalue(datas,sum/4+1,true);
q3 = findvalue(datas,sum*3/4+1,true);
break;
}
printf("%.1f\n", q3-q1);
return 0;
}

double findvalue(struct data* datas, int index, bool type){
int cot = 0, i =0;
while(cot+datas[i].frequency<index){
cot += datas[i].frequency;
i++;
}
if(type||cot+datas[i].frequency != index){
return datas[i].element*1.0;
}
else{
return (datas[i].element+datas[i+1].element)*1.0/2;
}
}```

## Problem solution in JavaScript programming.

```function processData(input) {
var n = input.split('\n')[0];
var x = input.split('\n')[1].split(' ').map(Number);
var f = input.split('\n')[2].split(' ').map(Number);
var data = [];
var L, U, Q1, Q3, IQR;

x.forEach(function(val, i) {
for(var z = 0; z < f[i]; z++) {
data.push(val);
}
});

data.sort(function(a, b) {
return a - b;
});

L = data.slice(0, data.length / 2);
U = data.slice(Math.round(data.length / 2));

Q1 = getMedian(L);
Q3 = getMedian(U);
IQR = parseFloat(Q3 - Q1).toFixed(1);

console.log(IQR);
}

function getMedian(arr) {
var median;

if (arr.length % 2 === 0) {
var midpoint = arr.length / 2
median = (arr[midpoint] + arr[midpoint - 1]) / 2
} else {
median = arr[Math.floor(arr.length / 2)];
}

return median;
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```