Calculate Median from given values of Mean and Mode
Last Updated :
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 = 6
Output: 4
Input: 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++
#include <bits/stdc++.h>
using namespace std;
void findMedian( int Mean, int Mode)
{
double Median = (2 * Mean + Mode) / 3.0;
cout << Median;
}
int main()
{
int mode = 6, mean = 3;
findMedian(mean, mode);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMedian( int Mean, int Mode)
{
double Median = ( 2 * Mean + Mode) / 3.0 ;
System.out.print(( int )Median);
}
public static void main (String[] args)
{
int mode = 6 , mean = 3 ;
findMedian(mean, mode);
}
}
|
Python3
def findMedian(Mean, Mode):
Median = ( 2 * Mean + Mode) / / 3
print (Median)
Mode = 6
Mean = 3
findMedian(Mean, Mode)
|
C#
using System;
class GFG
{
static void findMedian( int Mean, int Mode)
{
double Median = (2 * Mean + Mode) / 3.0;
Console.Write(Median);
}
public static void Main()
{
int mode = 6, mean = 3;
findMedian(mean, mode);
}
}
|
Javascript
<script>
function findMedian(Mean, Mode)
{
var Median = (2 * Mean + Mode) / 3.0;
document.write(Median);
}
var mode = 6, mean = 3;
findMedian(mean, mode);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...