The median of a list of numbers is essentially its middle element after sorting. The same number of elements occur after it as before. Given a list of numbers with an odd number of elements, find the median?
Example
arr = [5,3,1,2,4]
The sorted array arr’=[1,2,3,4,5]. The middle element and the median is 3.
Function Description
Complete the findMedian function in the editor below.
findMedian has the following parameter(s):
- int arr[n]: an unsorted array of integers
Returns
- int: the median of the array
Input Format
The first line contains the integer n, the size of arr.
The second line contains n space-separated integers arr[i].
Constraints
- 1<=n<=1000001
- N is odd
- 10000<=arr[i]<=10000
Sample Input 0
7 0 1 2 4 6 5 3
Sample Output 0
3
Explanation 0
The sorted arr = [0,1,2,3,4,5,6]. It’s middle element is at arr[3]=3.
TABLE OF CONTENTS
Find the Median HackerRank Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <assert.h> int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } /* Tail starts here */ int main() { int _ar_size; scanf("%d", &_ar_size); int _ar[_ar_size], _ar_i; for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) { scanf("%d", &_ar[_ar_i]); } qsort (_ar, _ar_size, sizeof(int), compare); printf("%d",_ar[_ar_size/2]); return 0; }
Find the Median HackerRank Solution in C++
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n, *arr; cin>>n; arr = new int[n]; for (int i = 0 ; i < n ; i++) cin>>arr[i]; sort(arr,arr+n); if (n%2 == 1) cout<<arr[(n-1)/2]<<endl; else cout<<(arr[n/2 - 1]+arr[n/2])/2<<endl; return 0; }
Find the Median HackerRank Solution in Java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(s[i]); } Arrays.sort(a); System.out.println(a[n/2]); } }
Find the Median HackerRank Solution in Python
# Enter your code here. Read input from STDIN. Print output to STDOUT N=raw_input() N=int(N) numbers=[] numbersInput=raw_input() for num in numbersInput.split(): numbers.append(int(num)) numbers.sort() print numbers[len(numbers)/2]
Find the Median HackerRank Solution in C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int n = int.Parse( Console.ReadLine()); int ans = Console.ReadLine().Trim().Split(' ').Select(int.Parse).OrderBy(i => i).Skip(n / 2).First(); Console.WriteLine(ans); } } }
Attempt Find the Median Hackerrank Challenge
Link – https://www.hackerrank.com/challenges/find-the-median/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/determining-dna-health-hackerrank-solution/