![]() |
VOOZH | about |
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 = 5Input: 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
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:
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: