Day 1: Interquartile Range | 10 Days Of Statistics | HackerRank Solution

Hello coders, today we are going to solve Day 1: Interquartile Range HackerRank Solution which is a part of 10 Day of Statistics Series.

Day 1: Interquartile Range

Objective

In this challenge, we practice calculating the interquartile range.

Task

The interquartile range of an array is the difference between its first (Q1) and third (Q3) quartiles (i.e., Q3Q1 ).

Given an array, values, of n integers and an array, freqs, representing the respective frequencies of values‘s elements, construct a data set, S, where each value[i] occurs at frequency freqs[i]. Then calculate and print S‘s interquartile range, rounded to a scale of 1 decimal place (i.e., 12.3 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.

Example

values = [1, 2, 3]
freqs = [3, 2, 1]
Apply the frequencies to the values to get the expanded array S = [1, 1, 1, 2, 2, 3]. Here left = [1, 1, 1], right = [2, 2, 3]. The median of the left half, Q1 = 1.0, the middle element. For the right half, Q3 = 2.0. Print the difference to one decimal place: Q3 Q1 = 2.0 – 1.0 = 1, so print 1.0.

Function Description

Complete the interQuartile function in the editor below.

interQuartile has the following parameters:
– int values[n]: an array of integers
– int freqs[n]: values[i] occurs freqs[i] times in the array to analyze

Prints

  • float: the interquartile range to 1 place after the decimal

Input Format

The first line contains an integer, n, the number of elements in arrays values and freqs.
The second line contains n space-separated integers describing the elements of array values.
The third line contains n space-separated integers describing the elements of array freqs.

Constraints

  • 5 <= n <= 50
  • 0 < values[i] <= 100
  • 0 < freqs[i] < 103
  • The number of elements in S is equal to freqs.

Output Format

Print the interquartile range for the expanded data set on a new line. Round the answer to a scale of 1 decimal place (i.e., 12.3 format).

Sample Input

STDIN           Function
-----           --------
6               arrays size n = 6
6 12 8 10 20 16 values = [6, 12, 8, 10, 20, 16]
5 4 3 2 1 5     freqs = [5, 4, 3, 2, 1, 5]

Sample Output

9.0

Explanation

First, we create data set S containing the data from set values at the respective frequencies specified by freqs:

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 Q1. There are 10 elements in lower half, so Q1 is the average of the middle two elements: 6 and 8. Thus, Q1 = 6+8/2 = 7.0.

Next, we find Q3. There are 10 elements in upper half, so Q3 is the average of the middle two elements: 16 and 16. Thus, Q3 = 16+16/2 = 16.0.

From this, we calculate the interquartile range as Q3Q1 = 16.0 – 7.0 = 9.0 and print 9.0 as our answer.

Solution – Day 1: Interquartile Range Solution

C++

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

double quad(vector<int> a)
{
    double q; int n=a.size(); 
    
    if(n%2==0) 
        q = (double)(a[n/2-1] + a[n/2])/2;
    else 
        q = (double)a[n/2];
    
    return q;
}

int main() 
{
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    
    int n; cin>>n; vector<int> a(n), f(n);
    
    for(auto i=0; i<n; i++)
        cin>>a[i];
    
    int sum=0;
    for(auto i=0; i<n; i++)
    {
        cin>>f[i];
        sum += f[i];
    }
    
    vector<int> s(sum);
    int idx=0;
    for(auto i=0; i<n; i++)
    {
        for(auto j=0; j<f[i]; j++)
        {
            s[idx] = a[i];
            idx++;
        }
    }
    
    sort(s.begin(), s.end());
    
    vector<int> l(s.begin(), s.begin()+sum/2);
    vector<int> h;
    
    if(sum%2==0)
    {
        h.insert(h.end(), s.begin()+sum/2, s.end());
    }
    else
    {
        h.insert(h.end(), s.begin()+sum/2+1, s.end());
    }
    
    double Q1 = quad(l);
    
    double Q3 = quad(h);
    
    printf("%.1f", (Q3-Q1));
    
    return 0;
}

Python

# Define functions
def median(size, values):
    if size % 2 == 0:
        median = (values[int(size/2)-1] + values[int(size/2)]) / 2
    else:
        median = float(values[int(size/2)])
    return median

# Set the data
size = int(input())
elements = list(map(int, input().split()))
frequencies = list(map(int, input().split()))

new_data = []
for i in range(len(elements)):
    for j in range(frequencies[i]):
        new_data.append(elements[i])
new_data = sorted(new_data)

size = int(len(new_data))
if size % 2 == 0:
    data_low = new_data[0:int(size/2)]
    data_high = new_data[int(size/2):size]
else:
    data_low = new_data[0:int(size/2)]
    data_high = new_data[int(size/2)+1:size]

low = median(len(data_low), data_low)
high = median(len(data_high), data_high)
print(high - low)

Disclaimer: The above Problem (Interquartile Range Solution) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *