
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Median with Given Mean and Mode in C++
Mean, Median, and Mode are fundamental statistical measures that are used to describe the central tendency and also explain the spread of the sheet. These three measures help in understanding the overall distribution of the data. In this article, we are going to learn how to find the Median if we are given the Mean and Mode of an array in C++.
Problem Statement
We are given an array of numbers, and we need to calculate the Median, in case when Mean and Mode of an array are given in C++.
Example
Input
[5, 15, 25, 35, 35, 40, 10]
Mean = 20
Mode = 35
Output
Median = 25
Brute Force Approach
In the brute force approach, we calculate the median by first sorting the array and then finding the middle element. If the number of elements in the array is odd, we directly return the middle element as the median, and if the number of elements is even, the median will be the average of the two middle elements.
Steps
- We first sort the array in ascending order.
- Now, we check if the number of elements is odd or even.
- If the number of elements is odd, then the middle element is the median.
- If the number of elements is even, we calculate the average of the two middle elements.
- Return the calculated median.
Implementation Code
This is the following implemented code for the above problem statement:
#include <bits/stdc++.h> using namespace std; // Function double calculateMedian(int arr[], int n) { sort(arr, arr + n); if (n % 2 != 0) { return arr[n / 2]; } else { return (arr[(n - 1) / 2] + arr[n / 2]) / 2.0; } } int main() { int arr[] = {5, 15, 25, 35, 35, 40, 10}; int n = 7; double median = calculateMedian(arr, n); cout << "The Median of the given dataset is: " << median << endl; return 0; }
Output
The Median of the given dataset is: 25
Time Complexity: O(n log n), as we are sorting the array.
Space Complexity: O(1), constant space
Optimized Approach
If we want to find the median of the array, we can use the direct formula if we are given the mean and mode of the array. This is the easiest way to find the median of an array. The formula to calculate the median when the mean and mode are known is:
Median = (3 Ã Mean ? Mode) / 2
We can use the above formula to calculate the median if the mean and mode are given.
Steps
- We define a function to calculate the median.
- We pass the values of mean and mode to this function.
- Now, we calculate the median using the formula: Median = (3 Ã Mean ? Mode) / 2
- Return the calculated median.
Implementation Code
#include <bits/stdc++.h> using namespace std; // Function double calculateMedian(double mean, double mode) { return (3 * mean - mode) / 2; } int main() { double mean, mode; cout << "Enter the mean of the array: "; cin >> mean; cout << "Enter the mode of the array: "; cin >> mode; double median = calculateMedian(mean, mode); // Output the result cout << "The Median of the array is: " << median << endl; return 0; }
Output
Enter the mean of the array: 20 Enter the mode of the array: 35 The Median of the array is: 25
Time Complexity: O(1), constant time.
Space Complexity: O(1), constant space.