Given two integers mean and mode, representing the Mean and Mode of a random group of data, the task is to calculate the median of that group of data.
Input: mean = 3, mode = 6
Output: 4Input: mean = 1, mode = 1
Output : 1
Approach: The given problem can be solved by using the mathematical relationship between mean, mode, and median of the group of data. Below is the relationship among them:
=>
=>
Therefore, the idea is to use the above formula to find the median of the data when the mean and the mode are given.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to find the median of a// group of data with given mean and modevoid findMedian(int Mean, int Mode){ // Calculate the median double Median = (2 * Mean + Mode) / 3.0; // Print the median cout << Median;}// Driver Codeint main(){ int mode = 6, mean = 3; findMedian(mean, mode); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Function to find the median of a// group of data with given mean and modestatic void findMedian(int Mean, int Mode){ // Calculate the median double Median = (2 * Mean + Mode) / 3.0; // Print the median System.out.print((int)Median);}// Driver codepublic static void main (String[] args){ int mode = 6, mean = 3; findMedian(mean, mode);}}// This code is contributed by code_hunt |
Python3
# Python3 program for the above approach# Function to find the median of# a group of data with given mean and modedef findMedian(Mean, Mode): # Calculate the median Median = (2 * Mean + Mode) // 3 # Print the median print(Median)# Driver codeMode = 6Mean = 3findMedian(Mean, Mode)# This code is contributed by virusbuddah |
C#
// C# program for the above approachusing System;class GFG{ // Function to find the median of a// group of data with given mean and modestatic void findMedian(int Mean, int Mode){ // Calculate the median double Median = (2 * Mean + Mode) / 3.0; // Print the median Console.Write(Median);}// Driver Codepublic static void Main(){ int mode = 6, mean = 3; findMedian(mean, mode);}}// This code is contributed by ipg2016107. |
Javascript
<script>// Javascript program for the above approach // Function to find the median of a// group of data with given mean and modefunction findMedian(Mean, Mode){ // Calculate the median var Median = (2 * Mean + Mode) / 3.0; // Print the median document.write(Median);}// Driver Codevar mode = 6, mean = 3;findMedian(mean, mode);// This code is contributed by Ankita saini</script> |
4
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Here you will find 95700 more Information on that Topic: geeksforgeeks.org/calculate-median-from-given-values-of-mean-and-mode/ […]
… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/calculate-median-from-given-values-of-mean-and-mode/ […]
… [Trackback]
[…] Read More Information here on that Topic: geeksforgeeks.org/calculate-median-from-given-values-of-mean-and-mode/ […]
… [Trackback]
[…] There you can find 55562 additional Information on that Topic: geeksforgeeks.org/calculate-median-from-given-values-of-mean-and-mode/ […]
… [Trackback]
[…] Info to that Topic: geeksforgeeks.org/calculate-median-from-given-values-of-mean-and-mode/ […]
… [Trackback]
[…] There you will find 61522 more Info on that Topic: geeksforgeeks.org/calculate-median-from-given-values-of-mean-and-mode/ […]
… [Trackback]
[…] Here you will find 44882 more Information on that Topic: geeksforgeeks.org/calculate-median-from-given-values-of-mean-and-mode/ […]