Given an unsorted array arr[], the task is to find the mean of the array.
Note: Return the floor value of the mean.
Examples :
Input: arr[] = [1, 3, 4, 2, 6, 5, 8, 7]
Output: 4
Explanation: Sum of the elements is 1 + 3 + 4 + 2 + 6 + 5 + 8 + 7 = 36,
Mean = 36/8 = 4.5.
Floor(4.5) = 4.
Input: arr[] = [4, 4, 4, 4, 4]
Output: 4
Explanation: Sum of the elements is 4 + 4 + 4 + 4 + 4 = 20,
Mean = 20/5 = 4
Naive Approach - O(n) Time O(1) Space
The idea is to traverse the array to compute the sum, then divide it by n to get the average. Since the return type is
int, the result is the floor of the mean.
#include <iostream>
using namespace std;
// Function to calculate the mean of an array
int findMean(vector<int> &arr)
{
int sum = 0;
int n = arr.size();
// Loop through the array and add each element to the sum
for (int i = 0; i < n; i++)
sum += arr[i];
int ans = sum / n;
return ans;
}
// Driver code
int main()
{
vector<int> arr = {1, 3, 4, 2, 6, 5, 8, 7};
cout << findMean(arr);
return 0;
}
#include <stdio.h>
// Function to calculate the mean of an array
int findMean(int arr[], int n)
{
int sum = 0;
// Loop through the array and add each element to the sum
for (int i = 0; i < n; i++)
sum += arr[i];
int ans = sum / n;
return ans;
}
// Driver code
int main()
{
int arr[] = {1, 3, 4, 2, 6, 5, 8, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", findMean(arr, n));
return 0;
}
import java.util.*;
// Function to calculate the mean of an array
class GfG {
public static int findMean(ArrayList<Integer> arr)
{
int sum = 0;
int n = arr.size();
// Loop through the array and add each element to the sum
for (int i = 0; i < n; i++)
sum += arr.get(i);
int ans = sum / n;
return ans;
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 3, 4, 2, 6, 5, 8, 7));
System.out.println(GfG.findMean(arr));
}
}
# Function to calculate the mean of an array
def findMean(arr):
sum = 0 # Initialize the sum variable to 0
n = len(arr)
# Loop through the array and add each element to the sum
for i in range(n):
sum += arr[i]
ans = sum // n
return ans
# Driver code
if __name__ == "__main__":
arr = [1, 3, 4, 2, 6, 5, 8, 7]
print(findMean(arr))
using System;
using System.Collections.Generic;
// Function to calculate the mean of an array
class GfG
{
public static int findMean(int[] arr)
{
int sum = 0; // Initialize the sum variable to 0
int n = arr.Length;
// Loop through the array and add each element to the sum
for (int i = 0; i < n; i++)
sum += arr[i];
int ans = sum / n;
return ans;
}
static void Main()
{
int[] arr = { 1, 3, 4, 2, 6, 5, 8, 7 };
Console.WriteLine(GfG.findMean(arr));
}
}
// Function to calculate the mean of an array
function findMean(arr)
{
let sum = 0; // Initialize the sum variable to 0
let n = arr.length;
// Loop through the array and add each element to the sum
for (let i = 0; i < n; i++)
sum += arr[i];
let ans = Math.floor(sum / n);
return ans;
}
// driver code
let arr = [1, 3, 4, 2, 6, 5, 8, 7];
console.log(findMean(arr));
Output
4
Time Complexity: O(n)
Auxiliary Space: O(1)
Handling Overflow - O(n) Time O(1) Space
Instead of computing the total sum (which may cause overflow), update the average incrementally while traversing the array. For each element, adjust the current average using the difference between the element and the current average, divided by its position. This avoids large intermediate sums and is numerically stable.
Let us understand with an example:
Input: arr[] = [1, 3, 4, 2, 6, 5, 8, 7]
Start traversal: Initialize avg = 0
- At 1 -> avg = 1
- At 3 -> avg = 1 + (3 - 1)/2 = 2
- At 4 -> avg = 2 + (4 - 2)/3 = 2.67
- At 2 -> avg = 2.67 + (2 - 2.67)/4 = 2.5
- At 6 -> avg = 3.2
- At 5 -> avg = 3.5
- At 8 -> avg ≈ 4.14
- At 7 -> avg = 4.5
All elements processed. Final value = floor(4.5) = 4
#include <bits/stdc++.h>
using namespace std;
// function for handling overflow
int findMean(vector<int> &arr)
{
long double avg = 0;
int n = arr.size();
for (int i = 0; i < n; i++)
{
// Update avg
avg += (arr[i] - avg) / (i + 1);
}
return (int)avg;
}
// Driver Code
int main()
{
vector<int> arr = {1, 3, 4, 2, 6, 5, 8, 7};
cout << findMean(arr);
return 0;
}
#include <stdio.h>
// function for handling overflow
int findMean(int arr[], int n)
{
long double avg = 0;
for (int i = 0; i < n; i++)
{
avg += (arr[i] - avg) / (i + 1);
}
return (int)avg;
}
// Driver Code
int main()
{
int arr[] = {1, 3, 4, 2, 6, 5, 8, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", findMean(arr, n));
return 0;
}
import java.util.*;
// solution class
class GfG {
// function for handling overflow
public static int findMean(ArrayList<Integer> arr) {
double avg = 0;
int n = arr.size();
for (int i = 0; i < n; i++) {
avg += (arr.get(i) - avg) / (i + 1);
}
return (int)avg;
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>(
Arrays.asList(1, 3, 4, 2, 6, 5, 8, 7)
);
System.out.print(findMean(arr));
}
}
# function for handling overflow
def findMean(arr):
avg = 0
n = len(arr)
for i in range(n):
avg += (arr[i] - avg) / (i + 1)
return int(avg)
# Driver Code
if __name__ == "__main__":
arr = [1, 3, 4, 2, 6, 5, 8, 7]
print(findMean(arr))
using System;
using System.Collections.Generic;
class GfG
{
// function for handling overflow
public static int findMean(int[] arr)
{
double avg = 0;
int n = arr.Length;
for (int i = 0; i < n; i++)
{
avg += (arr[i] - avg) / (i + 1);
}
return (int)avg;
}
public static void Main()
{
int[] arr = {1, 3, 4, 2, 6, 5, 8, 7};
Console.Write(findMean(arr));
}
}
// function for handling overflow
function findMean(arr) {
let avg = 0;
let n = arr.length;
for (let i = 0; i < n; i++) {
avg += (arr[i] - avg) / (i + 1);
}
return Math.floor(avg);
}
// driver code
let arr = [1, 3, 4, 2, 6, 5, 8, 7];
console.log(findMean(arr));
Output
4
Time Complexity: O(n)
Auxiliary Space: O(1)