Mean and Median of an Array (original) (raw)
Last Updated : 22 Jul, 2025
Given an array **arr[] of positive integers, find the **Mean and **Median, and return the **floor of both values.
**Note: Mean is the average of all elements in the array and **Median is the middle value when the array is sorted, if the number of elements is even, it's the average of the two middle values.
**Examples:
**Input: arr[] = [1, 2, 19, 28, 5]
**Output: 11 5
**Explanation: Sorted array - [1, 2, 5, 19, 28], **Mean = (1 + 2 + 19 + 28 + 5) / 5 = 55 / 5 = 11, **Median = Middle element = **5**Input: arr[] = [2, 8, 3, 4]
**Output: 4 3
**Explanation: Sorted array - [2, 3, 4, 8], **Mean = (2 + 3 + 4 + 8) / 4 = 17 / 4 = 4.25, so floor(4.25) is 4, **Median = (3 + 4)/2 = 3.5, so floor(3.5) is 3
[Approach] Using Definition of Mean and Median
**Mean (Average) : The mean is calculated by adding all elements of the array and dividing the sum by the total number of elements.
Formula: Mean = (a0 + a1 + a2 + --- + an-1) / n
**Median: The median represents the middle value in a sorted array.
**Steps to find the median:
- Sort the array in non-decreasing order.
- If the number of elements is:
=> Odd: Median = value at the middle index, Median = an/2th element
=> Even: Median = average of the two middle elements, Median = (an/2-1 + an/2) / 2. C++ `
#include #include #include using namespace std;
int mean(vector& arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
// we can calculate sum by using this function
// accumulate(arr.begin(),arr.end(),0);
// floor of the mean
return sum / arr.size(); }
int median(vector& arr) {
int n = arr.size();
// sorting the arrat 'arr' using built in functions
sort(arr.begin(), arr.end());
int result = 0;
// if there are two middle element
if (n % 2 == 0) {
result = (arr[n / 2] + arr[(n / 2) - 1]) / 2;
}
// if there are only one middle element
else {
result = arr[n / 2];
}
return result;}
int main() {
vector<int> arr = {2, 3, 4, 8};
int meanValue = mean(arr);
int medianValue = median(arr);
cout << meanValue << " " << medianValue << endl;
return 0;}
C
#include <stdio.h> #include <stdlib.h>
// comparator for qsort int compare(const void* a, const void* b) { return ((int)a - (int)b); }
int mean(int arr[], int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; }
// floor of the mean
return sum / n;}
int median(int arr[], int n) {
// sorting function
qsort(arr, n, sizeof(int), compare);
int result = 0;
// if there are two middle element
if (n % 2 == 0) {
result = (arr[n / 2] + arr[(n / 2) - 1]) / 2;
}
// if there are only one middle element
else {
result = arr[n / 2];
}
return result;}
int main() { int arr[] = {2, 3, 4, 8}; int n = sizeof(arr) / sizeof(arr[0]);
int meanValue = mean(arr, n);
int medianValue = median(arr, n);
printf("%d %d\n", meanValue, medianValue);
return 0;}
Java
import java.util.Arrays;
class GfG {
public static int mean(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
// Floor of the mean
return sum / arr.length;
}
public static int median(int[] arr) {
int n = arr.length;
// sorting function
Arrays.sort(arr);
int result = 0;
// if there are two middle element
if (n % 2 == 0) {
result = (arr[n / 2] + arr[(n / 2) - 1]) / 2;
}
// if there are only one middle element
else {
result = arr[n / 2];
}
return result;
}
public static void main(String[] args) {
int[] arr = {2, 3, 4, 8};
int meanValue = mean(arr);
int medianValue = median(arr);
System.out.println(meanValue + " " + medianValue);
}}
Python
def mean(arr): sum_val = 0 for num in arr: sum_val += num
# we can also calculate sum by using
# this function -> sum(arr)
# Floor of the mean
return sum_val // len(arr)def median(arr): n = len(arr)
# sorting function
arr.sort()
result = 0
# if there are two middle element
if n % 2 == 0:
result = (arr[n // 2] + arr[(n // 2) - 1]) // 2
# if there are only one middle element
else:
result = arr[n // 2]
return resultif name == "main": arr = [2, 3, 4, 8]
meanValue = mean(arr)
medianValue = median(arr)
print(meanValue, medianValue)C#
using System;
class GfG {
public static int mean(int[] arr) {
int sum = 0;
foreach (int num in arr) {
sum += num;
}
// we can also calculate sum by using this function -> arr.Sum();
// Floor of the mean
return sum / arr.Length;
}
public static int median(int[] arr) {
int n = arr.Length;
// sorting function
Array.Sort(arr);
int result = 0;
// if there are two middle element
if (n % 2 == 0) {
result = (arr[n / 2] + arr[(n / 2) - 1]) / 2;
}
// if there are only one middle element
else {
result = arr[n / 2];
}
return result;
}
public static void Main() {
int[] arr = {2, 3, 4, 8};
int meanValue = mean(arr);
int medianValue = median(arr);
Console.WriteLine(meanValue + " " + medianValue);
}}
JavaScript
function mean(arr) { let sum = 0; for (let num of arr) { sum += num; } // Floor of the mean return Math.floor(sum / arr.length); }
function median(arr) { let n = arr.length;
// sorting function
arr.sort((a, b) => a - b);
let result = 0;
// if there are two middle element
if (n % 2 === 0) {
result = Math.floor((arr[n / 2] + arr[(n / 2) - 1]) / 2);
}
// if there are only one middle element
else {
result = arr[Math.floor(n / 2)];
}
return result;}
// Driver Code let arr = [2, 3, 4, 8];
let meanValue = mean(arr); let medianValue = median(arr);
console.log(meanValue + " " + medianValue);
`
**Time Complexity: O(n*log(n)), O(n) for summing all the elements, O(n*log(n)) for sorting the array.
**Auxiliary Space: O(1), as no extra space is used.
**We can use Library Methods for calculating sum:
- accumulatein C++ STL (Standard Template Library)
- sum in Python
- Stream average in Java
- reduce in JavaScript