给定两个整数mean和mode ,代表随机数据组的Mean和Mode,任务是计算该组数据的中位数。
Input: mean = 3, mode = 6
Output: 4
Input: mean = 1, mode = 1
Output : 1
方法:给定的问题可以通过使用之间的平均,模式的数学关系来解决,而中位数组数据。下面是它们之间的关系:
=>
=>
因此,我们的想法是在给出均值和众数时使用上述公式找到数据的中位数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the median of a
// group of data with given mean and mode
void findMedian(int Mean, int Mode)
{
// Calcluate 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;
}
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
输出:
4
时间复杂度: O(1)
辅助空间: O(1)