Se le da un rango de números, desde el número más bajo hasta el número más alto (ambos inclusive). Considere todos los factores de estos números excepto 1, la tarea es encontrar el factor que aparece la mayor cantidad de veces. Si más de un número tiene una frecuencia máxima, imprima cualquiera de ellos.
Ejemplos:
Input : lower=10 higher=14 Output : The factor with maximum frequency is 2. Input : lower=11 higher=11 Output : The factor with maximum frequency is 11.
Explicación:
Si tenemos un rango de números, digamos del 10 al 14. Entonces los factores distintos a 1 serán:
- 10 => 2, 5, 10
- 11 => 11
- 12 => 2, 3, 4, 6, 12
- 13 => 13
- 14 => 2, 7, 14
En el caso anterior, frecuencia(2)= 3, que es máxima al comparar con otros factores.
Enfoque:
1. Si menor = mayor, entonces todos los factores tendrán una frecuencia de 1. Entonces podemos imprimir cualquier número que sea factor, ya que cada número es un factor de sí mismo, entonces imprimimos el mismo número.
2. De lo contrario, imprimimos ‘2’ ya que siempre será cierto para un rango de números.
C++
// CPP program to find most common factor // in a range. #include <bits/stdc++.h> using namespace std; int mostCommon(int lower, int higher) { // Check whether lower number // and higher number are same if (lower == higher) return lower; else return 2; } // Driver Code int main() { int lower = 10; // Lower number int higher = 20; // Higher number printf("The most frequent factor %d\n", mostCommon(lower, higher)); return 0; }
Java
// Java program to find most common factor // in a range. public class GfG { public static int mostCommon(int lower, int higher) { // Check whether lower number // and higher number are same if (lower == higher) return lower; else return 2; } // Driver code public static void main(String []args) { int lower = 10; // Lower number int higher = 20; // Higher number System.out.println("The most frequent factor " + mostCommon(lower, higher)); } } // This code is contributed by Rituraj Jain
Python 3
# Python 3 program to find most # common factor in a range. def mostCommon( lower, higher): # Check whether lower number # and higher number are same if (lower == higher): return lower else: return 2 # Driver Code lower = 10 # Lower number higher = 20 # Higher number print("The most frequent factor", mostCommon(lower, higher)) # This code is contributed by ash264
C#
// C# program to find most common factor // in a range. using System; class GFG { static int mostCommon(int lower, int higher) { // Check whether lower number // and higher number are same if (lower == higher) return lower; else return 2; } // Driver Code public static void Main() { int lower = 10; // Lower number int higher = 20; // Higher number Console.WriteLine("The most frequent factor " + mostCommon(lower, higher)); } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP program to find most common // factor in a range. function mostCommon($lower, $higher) { // Check whether lower number // and higher number are same if ($lower == $higher) return $lower; else return 2; } // Driver Code $lower = 10; // Lower number $higher = 20; // Higher number echo "The most frequent factor ". mostCommon($lower, $higher) . "\n"; // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // javascript program to find most common factor // in a range. function mostCommon(lower , higher) { // Check whether lower number // and higher number are same if (lower == higher) return lower; else return 2; } // Driver code var lower = 10; // Lower number var higher = 20; // Higher number document.write("The most frequent factor " + mostCommon(lower, higher)); // This code contributed by Princi Singh </script>
The most frequent factor 2
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)