C++ code to find minimum arithmetic mean deviation



The arithmetic mean deviation or mean absolute deviation is the summation of the absolute differences of each element and the mean of the dataset, then dividing it by the total number of data elements. To calculate the minimum arithmetic mean deviation, we used the median of the dataset in place of the mean.

The formula to calculate the minimum arithmetic mean deviation is given as:

$$ \text{MMD} = \frac{1}{n} \sum_{i=1}^{n} |x_i - M| = \frac{|x_1 - M| + |x_2 - M| + \cdots + |x_n - M|}{n} $$

Here are some example scenarios to calculate the minimum arithmetic mean deviation:

Scenario 1

Input: array = {2, 4, 5, 7, 9};
Output: 2
Explanation: 
Median of the array = 5
Sum of absolute differences from median:
= (|x1 - M| + |x2 - M| + |x3 - M| + |x4 - M| + |x5 - M|) / n
= (|2 - 5| + |4 - 5| + |5 - 5| + |7 - 5| + |9 - 5|) / 5
= (3 + 1 + 0 + 2 + 4) / 5 = 2

Scenario 2

Input: {3, 5, 8, 13, 20}
Output: 5
Explanation: 
Median of the array = 8
Sum of absolute differences from median:
= (|x1 - M| + |x2 - M| + |x3 - M| + |x4 - M| + |x5 - M|) / n
= (|3 - 8| + |5 - 8| + |8 - 8| + |13 - 8| + |20 - 8|) / 5
= (5 + 3 + 0 + 5 + 12) / 5 = 5

Steps to Find Minimum Arithmetic Mean Deviation

Here are the steps for calculating the minimum arithmetic mean deviation:

  • First, sort the array elements in increasing order to find the median.
  • If the number of elements is odd, then the median is the middle element in the sorted array.
  • If the number of elements is even, then we take the average of the middle element and its previous element.
  • Then find the absolute difference of each element from the median.
  • After finding the absolute difference of each element with the median, calculate their sum and divide this sum by the total number of elements.
  • The above step will return the minimum arithmetic mean deviation of the given elements.

Calculating Minimum Arithmetic Mean Deviation in C++

In this program, we have implemented the above-mentioned steps to find the minimum arithmetic mean deviation:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

double minDeviation(vector<int> &arr)
{
   int n = arr.size();
   if (n == 0)
   {
      return 0;
   }

   sort(arr.begin(), arr.end()); // For finding median
   double median;
   if (n % 2 == 0)
      median = (arr[n / 2 - 1] + arr[n / 2]) / 2.0; // for even number of elements
   else
      median = arr[n / 2]; // for odd number of elements

   // Computing sum of absolute deviations from Median
   double deviationSum = 0;
   for (int num : arr)
   {
      deviationSum += abs(num - median);
   }

   return deviationSum / n;
}

int main()
{
   vector<int> arr = {1, 2, 3, 4, 5};
   cout << "Given array: ";
   for (int num : arr)
   {
      cout << num << " ";
   }
   double amd = minDeviation(arr);

   cout << "\nMinimum Mean Deviation: " << amd << endl;

   return 0;
}

The output of the above code is as follows:

Given array: 1 2 3 4 5 
Minimum Mean Deviation: 1.2
Updated on: 2025-07-31T12:03:45+05:30

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements