Dada una array de 1 y 0, donde A i = 1 indica que el i -ésimo día fue un día lluvioso y A i = 0 indica que no fue un día lluvioso. La tarea es encontrar la probabilidad de que el N+1 sea un día lluvioso.
Ejemplos:
Entrada: a[] = {0, 0, 1, 0}
Salida: .25
Dado que un día fue lluvioso de 4 días, por lo tanto, la probabilidad en el
quinto día será 0.25 Entrada
: a[] = {1, 0, 1, 0, 1, 1, 1}
Salida: 0,71
La probabilidad de lluvia en el día N+1 se puede calcular mediante la siguiente fórmula:
Probability = number of rainy days / total number of days.
Primero, cuente el número de 1 y luego la probabilidad será el número de 1 dividido por N, es decir, cuenta / N.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code to find the probability of rain // on n+1-th day when previous day's data is given #include <bits/stdc++.h> using namespace std; // Function to find the probability float rainDayProbability(int a[], int n) { float count = 0, m; // count 1 for (int i = 0; i < n; i++) { if (a[i] == 1) count++; } // find probability m = count / n; return m; } // Driver Code int main() { int a[] = { 1, 0, 1, 0, 1, 1, 1, 1 }; int n = sizeof(a) / sizeof(a[0]); cout << rainDayProbability(a, n); return 0; }
Java
// Java code to find the // probability of rain // on n+1-th day when previous // day's data is given import java.io.*; import java.util.*; class GFG { // Function to find // the probability static float rainDayProbability(int a[], int n) { float count = 0, m; // count 1 for (int i = 0; i < n; i++) { if (a[i] == 1) count++; } // find probability m = count / n; return m; } // Driver Code public static void main(String args[]) { int a[] = { 1, 0, 1, 0, 1, 1, 1, 1 }; int n = a.length; System.out.print(rainDayProbability(a, n)); } }
Python 3
# Python 3 program to find # the probability of rain # on n+1-th day when previous # day's data is given # Function to find the probability def rainDayProbability(a, n) : # count occurrence of 1 count = a.count(1) # find probability m = count / n return m # Driver code if __name__ == "__main__" : a = [ 1, 0, 1, 0, 1, 1, 1, 1] n = len(a) # function calling print(rainDayProbability(a, n)) # This code is contributed # by ANKITRAI1
C#
// C# code to find the // probability of rain // on n+1-th day when // previous day's data // is given using System; class GFG { // Function to find // the probability static float rainDayProbability(int []a, int n) { float count = 0, m; // count 1 for (int i = 0; i < n; i++) { if (a[i] == 1) count++; } // find probability m = count / n; return m; } // Driver Code public static void Main() { int []a = {1, 0, 1, 0, 1, 1, 1, 1}; int n = a.Length; Console.WriteLine(rainDayProbability(a, n)); } } // This code is contributed // by inder_verma.
PHP
<?php // PHP code to find the // probability of rain // on n+1-th day when // previous day's data // is given // Function to find // the probability function rainDayProbability($a, $n) { $count = 0; $m; // count 1 for ($i = 0; $i <$n; $i++) { if ($a[$i] == 1) $count++; } // find probability $m = $count / $n; return $m; } // Driver Code $a = array(1, 0, 1, 0, 1, 1, 1, 1); $n = count($a); echo rainDayProbability($a, $n); // This code is contributed // by inder_verma. ?>
Javascript
<script> // JavaScript code to find the probability of rain // on n+1-th day when previous day's data is given // Function to find the probability function rainDayProbability(a,n) { let count = 0, m; // count 1 for (let i = 0; i < n; i++) { if (a[i] == 1) count++; } // find probability m = count / n; return m; } // Driver Code let a = [1, 0, 1, 0, 1, 1, 1, 1 ]; let n = a.length; document.write(rainDayProbability(a,n)); // This code contributed by Rajput-Ji </script>
Producción:
0.75
Complejidad de tiempo: O(N)