Header Ad

HackerRank Day 1: Quartiles | 10 Days of Statistics problem solution

In this Hackerrank Day 1: Quartiles 10 Days of Statistics problem we have Given an array of integers and we need to calculate the respective first quartile, second quartile, and third quartile. It is guaranteed that all the quartiles are integers.

HackerRank Day 1: Quartiles | 10 Days of Statistics problem solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
x = sorted(list(map(int, input().split())))
from statistics import median
print(int(median(x[:n//2])))
print(int(median(x)))
print(int(median(x[(n+1)//2:])))



Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int count = s.nextInt();
        int[] numbers = new int[count];
        for (int i = 0; i < count; i ++) {
            numbers[i] = s.nextInt();
        }
        Arrays.sort(numbers);
        int Q2Val = findMedianHelper(numbers, 0, numbers.length - 1);
        int Q1Val = findMedianHelper(numbers, 0, numbers.length / 2 - 1);
        int Q3Val = findMedianHelper(numbers, numbers.length % 2 == 0 ? numbers.length / 2 : numbers.length / 2 + 1, numbers.length - 1);
        

        System.out.format("%d\n%d\n%d\n", Q1Val, Q2Val, Q3Val);
    }
    
    public static int findMedianHelper(int[] array, int lo, int hi) {
        return (
        		array[(lo + hi) / 2] +
        		array[(lo + hi + 1) / 2]
        		)  / 2;
    }
}


Problem solution in C++ programming.

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


int median(vector<int>::iterator a, vector<int>::iterator b){
    int size = b - a;
    if(size % 2 == 0){
        return (*(a + size / 2 - 1) + *(a + size / 2)) / 2;
    }
    else{
        return *(a + size / 2);
    }
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n; cin >> n;
    vector<int> X(n);
    for(int i = 0; i < n; i++){
        cin >> X[i];
    }
    sort(X.begin(), X.end());
    
    int q2 = median(X.begin(), X.end());
    vector<int>::iterator bl = lower_bound(X.begin(), X.end(), q2);
    int q1 = median(X.begin(), bl);
    vector<int>::iterator bu = upper_bound(X.begin(), X.end(), q2);
    int q3 = median(bu, X.end());
    
    cout << q1 << endl;
    cout << q2 << endl;
    cout << q3 << endl;
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int my_cmp(const void *a, const void *b) {
    const int *ia = (const int *) a;
    const int *ib = (const int *) b;
    return *ia > *ib;
}

int main() {
    int n, numbers[101], i, h;
    int q1, q2, q3;
    scanf("%d", &n);
    for(i = 0; i < n; ++i) {
        scanf("%d", &numbers[i]);
    }
    qsort(numbers, n, sizeof(int), my_cmp);

    if(n & 1) {
        q2 = numbers[n/2];
        h = n / 2;
        if(h & 1) {
            q1 = numbers[h/2];
            q3 = numbers[h + h/2 + 1];
        }
        else {
            q1 = (numbers[h/2] + numbers[h/2 - 1] )/ 2;
            q3 = (numbers[h + h/2] + numbers[h + h/2 + 1]) / 2;
        }
    }
    else {
        q2 = (numbers[n/2] + numbers[n/2 - 1]) / 2;
        h = n / 2;
        if(h & 1) {
            q1 = numbers[h/2];
            q3 = numbers[h + h/2];
        }
        else {
            q1 = (numbers[h/2] + numbers[h/2 - 1])/2;
            q3 = (numbers[h + h/2] + numbers[h + h/2 - 1])/2;
        }
    }
    printf("%d\n%d\n%d", q1, q2, q3);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}


Problem solution in JavaScript programming.

function getMedian(values){
    var median = 0;
    var length = values.length;
    var midpoint = Math.floor((length-1)/2);

    //if even
    if ((length%2) == 0){
        median = (values[midpoint]+values[(midpoint)+1])/2;
    } else {
        median = values[midpoint];
    }
    return median;
}

function getQuartiles(values, length){    
    var midpoint = Math.floor(length/2);
    
    var offset = length%2;

    console.log(getMedian(values.slice(0,midpoint)));
    console.log(getMedian(values));
    console.log(getMedian(values.slice(midpoint+offset)));

}

function processData(input) {
    input = input.split('\n')
    var n = input[0];
    var x = input[1].split(' ');
    x = x.map(Number);
    x.sort(function(a,b){ return a-b; })

    getQuartiles(x,n);
} 

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

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


Post a Comment

0 Comments