In this Hackerrank Find the Median problem we have given a list of numbers with an odd number of elements and we need to find the median of that.

Hackerrank Find the Median problem solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'findMedian' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def findMedian(arr):
    arr = sorted(arr)
    return arr[len(arr)//2]

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    result = findMedian(arr)

    fptr.write(str(result) + '\n')

    fptr.close()


Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        try {
            br.readLine();
            String[] strArr = br.readLine().split(" ");
            if (strArr == null || strArr.length == 0) {
                return;
            }
            int[] intArr = strArrToIntArr(strArr);
            Arrays.sort(intArr);
            System.out.println(intArr[intArr.length / 2]);
        } catch (IOException e) {
            
        }
    }
    
    public static int[] strArrToIntArr(String[] strArr){
        if(strArr == null || strArr.length <= 0){ return null; }
        int[] intArr = new int[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            intArr[i] = Integer.parseInt(strArr[i]);
        }
        return intArr;
    }
}


Problem solution in C++ programming.

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


int main() {
    int n;
    cin >> n;
    int mv = 0;
    vector<int> counts(20001, 0);
    for (int i = 0; i < n; ++i) {
        int v;
        cin >> v;
        v += 10000;
        ++counts[v];
        mv = min(mv, v);    
    }
    int c = 0;
    for (int i = mv; i < counts.size(); ++i) {
        c += counts[i];
        if (c * 2 > n) {
            cout << i - 10000 << endl;
            break;
        }
    }
    return 0;
}


Problem solution in C programming.

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

int int_compare (const void *a, const void *b)
{
    return ( *(int*)a - *(int*)b);
}

int main() {
    
    int count;
    scanf("%d", &count);

    int arr[count];
    
    for (int i = 0; i < count; i++)
    {
        scanf("%d", &arr[i]);
    }
    
    qsort(arr, count, sizeof(int), int_compare);
    
    printf("%d", arr[count/2]);
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var input_arr = input.split('\n');
    var n = parseInt(input_arr[0],10);
    var arr = input_arr[1].split(' ');
    var freq_arr = {};
    var middle = Math.ceil(n/2);
    var count = 0;
    var min = parseInt(arr[0],10);
    var max = parseInt(arr[0],10);
    
    for(var i = 0; i < n; i++){
        var num = parseInt(arr[i],10);        
        !!freq_arr[num] ? freq_arr[num] += 1: freq_arr[num] = 1;
        if (num < min) { min = num}
        if (num > max) { max = num}
    }
    
    for (var i = min; i <= max; i++) {

        !!freq_arr[i] ? count += freq_arr[i] : null;
        if (count >= middle) {
            process.stdout.write(i);
            return;
        }
    }
    
} 

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

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