Find Mode if Mean and Median are Given of an Array in C++



Mean, median and mode are three basic concepts of statistics. These quantities help in understanding the central tendency and distribution of given data. In this article we are going to learn, how we can find the Mode if the Mean and Median of a given array (which is the same as ungrouped data) in C++

Problem statement

We are given an array of numbers and we have to find the mode if mean and median are given in C++. 

Example

Input

[5, 15, 25, 35, 35, 40, 10]

Mean = 20
Median = 25

Output

35

Brute Force Approach

Mode is the most frequently occurring value. We can traverse the array and return the element having the highest frequency, but this will take O(n) time as we have to traverse the complete array. 

Steps

  • We use an unordered map to find the frequency of each element.
  • Now, we store the count of each element.
  • We initialize two variables mode with the first element of the array and maxCount to 0.
  • We loop through each key-value pair in map. For each element if its frequency is greater than maxCount, update maxCount and set mode to the current element.
  • Finally, we return the mode of the array.

Implementation Code

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

// Function to find the mode
int findMode(const vector < int > & arr, int n) {
  unordered_map < int, int > mp;

  for (int i = 0; i < n; i++) {
    mp[arr[i]]++;
  }

  int mode = arr[0];
  int maxCount = 0;

  for (const auto & x: mp) {
    int element = x.first;
    int count = x.second;
    if (count > maxCount) {
      maxCount = count;
      mode = element;
    }
  }
  // Return the mode
  return mode;
}

int main() {
  vector < int > arr = {5,15,25,35,35,40,10};
  int n = arr.size();
  int mode = findMode(arr, n);

  cout << "The Mode of the given dataset is: " << mode << endl;

  return 0;
}

Output:

The Mode of the given dataset is: 35

Time Complexity: O(n), as we use the map to store each element's frequency.

Space Complexity: O(1), constant space

Optimized Approach

If we are given the mean and median of the array, then we can simply use the direct formula to find the mode of the array instead of traversing the complete array. 

According to the formula:

Mode = 3 × Median ? 2 × Mean

We can simply use the above formula to find the value of mode if mean and median are given of an array. 

Steps

  • We will define a function to find the mode of the given array.
  • This function will take two arguments, mean and median, and find the mode using a direct formula.
  • We can calculate Mode using direct formula: Mode = 3 × Median ? 2 × Mean.

Implementation Code

#include <iostream>
using namespace std;

// Function to calculate mode
double calculateMode(double mean, double median) {
    return 3 * median - 2 * mean;
}

int main() {
    double mean, median;

    // Input mean and median
    cout << "Enter the mean of the array: ";
    cin >> mean;
    cout << "Enter the median of the array: ";
    cin >> median;

    // Calculate mode using the formula
    double mode = calculateMode(mean, median);

    // Output the result
    cout << "The mode of the array is: " << mode << endl;

    return 0;
}

Output:

Enter the mean of the array: 10
Enter the median of the array: 15
The mode of the array is: 25

Time Complexity: O(1), we are using constant space.

Space Complexity: O(1), as no additional memory is used.

Updated on: 2024-11-10T13:49:04+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements