Calculate Median from given values of Mean and Mode - GeeksforGeeks

18 Aug, 2021
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 = 6Output: 4
Input: mean = 1, mode = 1Output : 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++
Java
Python3
C#
Javascript
// 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 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 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 = 3 findMedian(Mean, Mode) # This code is contributed by virusbuddah
// 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.
<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)
virusbuddha
ankita_saini
ipg2016107
code_hunt
singghakshay
maths-mean
median-finding
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Fizz Buzz Implementation
Program to multiply two matrices
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair