Day 1: Quartiles | 10 Days Of Statistics | HackerRank Solution

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

Day 1: Quartiles

Objective

In this challenge, we practice calculating quartiles.

Task

Given an array, arr, of n integers, calculate the respective first quartile (Q1), second quartile (Q2), and third quartile (Q3). It is guaranteed that Q1Q2, and Q3 are integers.

Example

arr = [9, 5, 7, 1, 3]
The sorted array is [1, 3, 5, 7, 9] which has an odd number of elements. The lower half consists of [1, 3], and its median is 1+3/2 = 2. The middle element is 5 and represents the second quartile. The upper half is [5, 7] and its median is 5+7/2 = 6. Return [2, 5, 8].

arr = [1, 3, 5, 7]
The array is already sorted. The lower half is [1, 3] with a median = 1+3/2 = 2. The median of the entire array is 3+5/2 = 4, and of the upper half is 5+7/2 = 6. Return [2, 4, 6].

Function Description

Complete the quartiles function in the editor below.

quartiles has the following parameters:

  • int arr[n]: the values to segregate

Returns

  • int[3]: the medians of the left half of arrarr in total, and the right half of arr.

Input Format

The first line contains an integer, n, the number of elements in arr.
The second line contains n space-separated integers, each an arr[i].

Constraints

  • 5 <= n <= 50
  • 0 < arr[i] <= 100, where arr[i] is the ith element of the array.

Sample Input

STDIN                   Function
-----                   --------    
9                       arr[] size n = 9 
3 7 8 5 12 14 21 13 18  arr = [3, 7, 8, 5, 12, 14, 21, 13,18]

Sample Output

6
12
16

Explanation

arrsorted = [3, 5, 7, 8, 12, 13, 14, 18, 21]. There is an odd number of elements, and the middle element, the median, is 12.

As there are an odd number of data points, we do not include the median (the central value in the ordered list) in either half:

Lower half (L): 3, 5, 7, 8

Upper half (U): 13, 14, 18, 21

Solution – Day 1: Quartiles Solution

C++

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

int quad(vector<int> a)
{
    int q, n=a.size(); 
    
    if(n%2==0) 
        q = (a[n/2-1] + a[n/2])/2;
    else 
        q = 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);
    
    for(auto i=0; i<n; i++)
        cin>>a[i];
    
    sort(a.begin(), a.end());
    
    int Q1, Q2, Q3;
    
    Q2 = quad(a);
    
    vector<int> l(a.begin(), a.begin()+n/2);
    vector<int> h;
    
    if(n%2==0)
    {
        h.insert(h.end(), a.begin()+n/2, a.end());
    }
    else
    {
        h.insert(h.end(), a.begin()+n/2+1, a.end());
    }
    
    Q1 = quad(l);
    
    Q3 = quad(h);
    
    cout<<Q1<<"\n"<<Q2<<"\n"<<Q3<<"\n";
        
    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 = values[int(size/2)]
    return int(median)

size = int(input())
numbers = sorted(list(map(int, input().split())))

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

print (median(len(data_low), data_low))
print (median(size, numbers))
print (median(len(data_high), data_high))

Disclaimer: The above Problem (Day 1: Quartiles) 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 *