Dada una array A de N elementos. Encuentre la frecuencia del valor más pequeño en la array.
Ejemplos:
Input : N = 5, arr[] = {3, 2, 3, 4, 4} Output : 1 The smallest element in the array is 2 and it occurs only once. Input : N = 6, arr[] = {4, 3, 5, 3, 3, 6} Output : 3 The smallest element in the array is 3 and it occurs 3 times.
Enfoque simple : un método simple es encontrar primero el elemento mínimo en la array en el primer recorrido. Luego recorra la array nuevamente y encuentre el número de ocurrencias del elemento mínimo.
Enfoque eficiente : el enfoque eficiente es hacer esto en un solo recorrido.
Supongamos que el primer elemento es el mínimo actual, por lo que la frecuencia del mínimo actual sería 1. Ahora iteremos a través de la array (1 a N), nos encontramos con 2 casos:
- El elemento actual es más pequeño que nuestro mínimo actual: cambiamos el mínimo actual para que sea igual al elemento actual y dado que es la primera vez que nos encontramos con este elemento, hacemos que su frecuencia sea 1.
- El elemento actual es igual al mínimo actual: Incrementamos la frecuencia del mínimo actual.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the frequency of // minimum element in the array #include <bits/stdc++.h> using namespace std; // Function to find the frequency of the // smallest value in the array int frequencyOfSmallest(int n, int arr[]) { int mn = arr[0], freq = 1; for (int i = 1; i < n; i++) { // If current element is smaller // than minimum if (arr[i] < mn) { mn = arr[i]; freq = 1; } // If current element is equal // to smallest else if (arr[i] == mn) freq++; } return freq; } // Driver Code int main() { int N = 5; int arr[] = { 3, 2, 3, 4, 4 }; cout << frequencyOfSmallest(N, arr); return 0; }
Java
// Java program to find the frequency of // minimum element in the array import java.io.*; class GFG { // Function to find the frequency of the // smallest value in the array static int frequencyOfSmallest(int n, int arr[]) { int mn = arr[0], freq = 1; for (int i = 1; i < n; i++) { // If current element is smaller // than minimum if (arr[i] < mn) { mn = arr[i]; freq = 1; } // If current element is equal // to smallest else if (arr[i] == mn) freq++; } return freq; } // Driver Code public static void main (String[] args) { int N = 5; int arr[] = { 3, 2, 3, 4, 4 }; System.out.println (frequencyOfSmallest(N, arr)); } } // This code is contributed by Tushil.
Python3
# Python 3 program to find the frequency of # minimum element in the array # Function to find the frequency of the # smallest value in the array def frequencyOfSmallest(n,arr): mn = arr[0] freq = 1 for i in range(1,n): # If current element is smaller # than minimum if (arr[i] < mn): mn = arr[i] freq = 1 # If current element is equal # to smallest elif(arr[i] == mn): freq += 1 return freq # Driver Code if __name__ == '__main__': N = 5 arr = [3, 2, 3, 4, 4] print(frequencyOfSmallest(N, arr)) # This code is contributed by # Surendra_Gangwar
C#
// C# program to find the frequency of // minimum element in the array using System; class GFG { // Function to find the frequency of the // smallest value in the array static int frequencyOfSmallest(int n, int []arr) { int mn = arr[0], freq = 1; for (int i = 1; i < n; i++) { // If current element is smaller // than minimum if (arr[i] < mn) { mn = arr[i]; freq = 1; } // If current element is equal // to smallest else if (arr[i] == mn) freq++; } return freq; } // Driver Code public static void Main() { int N = 5; int []arr = { 3, 2, 3, 4, 4 }; Console.WriteLine(frequencyOfSmallest(N, arr)); } } // This code is contributed by Ryuga
PHP
<?php // PHP program to find the frequency of // minimum element in the array // Function to find the frequency of the // smallest value in the array function frequencyOfSmallest($n, $arr) { $mn = $arr[0]; $freq = 1; for ($i = 1; $i < $n; $i++) { // If current element is smaller // than minimum if ($arr[$i] < $mn) { $mn = $arr[$i]; $freq = 1; } // If current element is equal // to smallest else if ($arr[$i] == $mn) $freq++; } return $freq; } // Driver Code $N = 5; $arr = array( 3, 2, 3, 4, 4 ); print(frequencyOfSmallest($N, $arr)); // This code is contributed by mits ?>
Javascript
<script> // Javascript program to find the frequency of // minimum element in the array // required answer // Function to find the frequency of the // smallest value in the array function frequencyOfSmallest(n, arr) { let mn = arr[0], freq = 1; for (let i = 1; i < n; i++) { // If current element is smaller // than minimum if (arr[i] < mn) { mn = arr[i]; freq = 1; } // If current element is equal // to smallest else if (arr[i] == mn) freq++; } return freq; } let N = 5; let arr = [ 3, 2, 3, 4, 4 ]; document.write(frequencyOfSmallest(N, arr)); </script>
1
Complejidad de tiempo: O(N)
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA