Probability of rain on N+1th day

Given an array of 1 and 0’s, where Ai = 1 denotes that ith day was a rainy day and Ai = 0 denotes it was not a rainy day. The task is to find the probability that the N+1th was a rainy day.

Examples:

Input: a[] = {0, 0, 1, 0}
Output: .25
Since one day was rainy out of 4 days, hence the probability on
5th day will be 0.25



Input: a[] = {1, 0, 1, 0, 1, 1, 1}
Output: 0.71

The probability of rain on N+1th day can be found out using the below formula:

Probability = number of rainy days / total number of days.

First, count the number of 1’s and then the probability will be the number of 1’s divided by N i.e. count / N.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
}
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 occurence 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right


Output:

0.75

Time Complexity: O(N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : inderDuMCA, AnkitRai01