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 mode void findMedian( int Mean, int Mode) { // Calculate the median double Median = (2 * Mean + Mode) / 3.0; // Print the median cout << Median; } // Driver Code int main() { int mode = 6, mean = 3; findMedian(mean, mode); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the median of a // group of data with given mean and mode static 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 code public 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 mode def findMedian(Mean, Mode): # Calculate the median Median = ( 2 * Mean + Mode) / / 3 # Print the median print (Median) # Driver code Mode = 6 Mean = 3 findMedian(Mean, Mode) # This code is contributed by virusbuddah |
C#
// C# program for the above approach using System; class GFG { // Function to find the median of a // group of data with given mean and mode static void findMedian( int Mean, int Mode) { // Calculate the median double Median = (2 * Mean + Mode) / 3.0; // Print the median Console.Write(Median); } // Driver Code public 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 mode function findMedian(Mean, Mode) { // Calculate the median var Median = (2 * Mean + Mode) / 3.0; // Print the median document.write(Median); } // Driver Code var 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!