Given a matrix M of order N*M where Mij represent the occurrence of j in row i. The task is to find the probability of occurrence of given k in last row after applying the given operation:
- Starting from 1st row, we select one element from any column of the entire row, and add it to the same column of next row. We repeating this till the last row.
Examples:
Input: k = 1, M[][] =
{{0, 1},
{1, 1}}
Output:0.666667
Input: k = 1, M[][] =
{{0, 1},
{1, 1}}
Output: 0.333333
Approach :
- Pre-calculate a sum[] array which stores the sum of all elements of 1st row.
- Fill 1st row of dp[][] by the first row of M[][] elements.
- Calculate the probability of selecting an element from each column of a particular row and add the same to the corresponding column.
- Also, update the Sum[] of the row while updating the value of dp[][] matrix.
- Finally, the value of dp[n][k] for given k is the required value for the probability of selecting element k after all iterations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define n 4
#define m 4
using namespace std;
float calcProbability( int M[][m], int k)
{
float dp[m][n], sum[n];
for ( int j = 0; j < n; j++) {
dp[0][j] = M[0][j];
sum[0] += dp[0][j];
}
for ( int i = 1; i < m; i++) {
for ( int j = 0; j < n; j++) {
dp[i][j] += dp[i - 1][j] / sum[i - 1] + M[i][j];
sum[i] += dp[i][j];
}
}
return dp[n - 1][k - 1] / sum[n - 1];
}
int main()
{
int M[m][n] = { { 1, 1, 0, 3 },
{ 2, 3, 2, 3 },
{ 9, 3, 0, 2 },
{ 2, 3, 2, 2 } };
int k = 3;
cout << calcProbability(M, k);
return 0;
}
|
Java
class GFG
{
final static int n = 4 ;
final static int m = 4 ;
static float calcProbability( int M[][], int k)
{
float dp[][] = new float [m][n] ;
float sum[] = new float [n];
for ( int j = 0 ; j < n; j++)
{
dp[ 0 ][j] = M[ 0 ][j];
sum[ 0 ] = sum[ 0 ] + dp[ 0 ][j];
}
for ( int i = 1 ; i < m; i++)
{
for ( int j = 0 ; j < n; j++)
{
dp[i][j] += dp[i - 1 ][j] / sum[i - 1 ] +
M[i][j];
sum[i] += dp[i][j];
}
}
return dp[n - 1 ][k - 1 ] / sum[n - 1 ];
}
public static void main(String []args)
{
int M[][] = { { 1 , 1 , 0 , 3 },
{ 2 , 3 , 2 , 3 },
{ 9 , 3 , 0 , 2 },
{ 2 , 3 , 2 , 2 } };
int k = 3 ;
System.out.println(calcProbability(M, k));
}
}
|
Python3
n = 4
m = 4
def calcProbability(M, k):
dp = [[ 0 for i in range (n)]
for i in range (m)]
Sum = [ 0 for i in range (n)]
for j in range (n):
dp[ 0 ][j] = M[ 0 ][j]
Sum [ 0 ] + = dp[ 0 ][j]
for i in range ( 1 , m):
for j in range (n):
dp[i][j] + = (dp[i - 1 ][j] /
Sum [i - 1 ] + M[i][j])
Sum [i] + = dp[i][j]
return dp[n - 1 ][k - 1 ] / Sum [n - 1 ]
M = [[ 1 , 1 , 0 , 3 ],
[ 2 , 3 , 2 , 3 ],
[ 9 , 3 , 0 , 2 ],
[ 2 , 3 , 2 , 2 ]]
k = 3
print (calcProbability(M, k))
|
C#
using System;
class GFG
{
static int n = 4 ;
static int m = 4 ;
static float calcProbability( int [,] M, int k)
{
float [,] dp = new float [m,n] ;
float [] sum = new float [n];
for ( int j = 0; j < n; j++)
{
dp[0, j] = M[0, j];
sum[0] = sum[0] + dp[0, j];
}
for ( int i = 1; i < m; i++)
{
for ( int j = 0; j < n; j++)
{
dp[i, j] += dp[i - 1,j] / sum[i - 1] +
M[i, j];
sum[i] += dp[i, j];
}
}
return dp[n - 1,k - 1] / sum[n - 1];
}
public static void Main()
{
int [,] M = { { 1, 1, 0, 3 },
{ 2, 3, 2, 3 },
{ 9, 3, 0, 2 },
{ 2, 3, 2, 2 } };
int k = 3;
Console.Write(calcProbability(M, k));
}
}
|
PHP
<?php
function calcProbability( $M , $k )
{
$m = 4; $n = 4;
$dp = array ();
$sum = array ();
for ( $j = 0; $j < $n ; $j ++)
{
$dp [0][ $j ] = $M [0][ $j ];
$sum [0] += $dp [0][ $j ];
}
for ( $i = 1; $i < $m ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
$dp [ $i ][ $j ] += $dp [ $i - 1][ $j ] /
$sum [ $i - 1] + $M [ $i ][ $j ];
$sum [ $i ] += $dp [ $i ][ $j ];
}
}
return $dp [ $n - 1][ $k - 1] / $sum [ $n - 1];
}
$M = array ( array (1, 1, 0, 3),
array (2, 3, 2, 3),
array (9, 3, 0, 2),
array (2, 3, 2, 2));
$k = 3;
echo calcProbability( $M , $k );
?>
|
Javascript
<script>
let n = 4 ;
let m = 4 ;
function calcProbability(M,k)
{
let dp = new Array(m) ;
let sum = new Array(n);
for (let i = 0; i < dp.length; i++)
{
dp[i] = new Array(n);
for (let j = 0; j < n; j++)
{
dp[i][j] = 0;
}
}
for (let i = 0; i < n; i++)
{
sum[i] = 0;
}
for (let j = 0; j < n; j++)
{
dp[0][j] = M[0][j];
sum[0] = sum[0] + dp[0][j];
}
for (let i = 1; i < m; i++)
{
for (let j = 0; j < n; j++)
{
dp[i][j] += dp[i - 1][j] / sum[i - 1] +
M[i][j];
sum[i] += dp[i][j];
}
}
return dp[n - 1][k - 1] / sum[n - 1];
}
let M = [[ 1, 1, 0, 3 ],
[ 2, 3, 2, 3 ],
[ 9, 3, 0, 2 ],
[ 2, 3, 2, 2 ]];
let k = 3;
document.write(calcProbability(M, k));
</script>
|
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!