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 = 3Â
findMedian(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/ […]