Dados dos enteros , la media y la moda , que representan la media y la moda de un grupo aleatorio de datos, la tarea es calcular la mediana de ese grupo de datos .
Entrada: media = 3, moda = 6
Salida: 4Entrada: media = 1, moda = 1
Salida : 1
Enfoque: el problema dado se puede resolver usando la relación matemática entre la media , la moda y la mediana del grupo de datos. A continuación se muestra la relación entre ellos:
=>
=>
Por lo tanto, la idea es usar la fórmula anterior para encontrar la mediana de los datos cuando se dan la media y la moda.
A continuación se muestra la implementación del enfoque anterior:
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
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por saragupta1924 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA