Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Hackerrank Find the Median problem solution

YASH PAL, 31 July 202430 November 2025

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);
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

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

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes