Open In App

Mean and Median of an Array

Last Updated : 27 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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:

  • The mean is calculated by summing all elements and dividing by the size of the array.
  • For the median, the array is first sorted; then the middle element is taken if the size is odd, or the average of the two middle elements if the size is even.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int mean(vector<int>& 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<int>& 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 result

def main():
    arr = [2, 3, 4, 8]

    meanValue = mean(arr)
    medianValue = median(arr)

    print(meanValue, medianValue)

if __name__ == "__main__":
    main()
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;
}

let arr = [2, 3, 4, 8];

let meanValue = mean(arr);
let medianValue = median(arr);

console.log(meanValue + " " + medianValue);

Output
4 3

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:


Next Article

Similar Reads