-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProbability.java
More file actions
63 lines (55 loc) · 1.48 KB
/
Probability.java
File metadata and controls
63 lines (55 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Given n matches, and corresponding probability of winning of Indian cricket team in that match.
Given k matches to win, ouptut the probability of winning exactly k matches.
Input Format:
-------------
Line-1 -> An integer N
Line-2 -> N space seperated double values, 0.0 <= value <=1.0
Line-3 -> An integer K, 0<= K <=N
Output Format:
--------------
Print the result (double value).
Sample Input-1:
---------------
1
0.4
0
Sample Output-1:
----------------
0.6
Sample Input-2:
---------------
4
0.5 0.5 0.5 0.5
2
Sample Output-2:
----------------
0.375*/
import java.util.*;
class Probability{
static float dp[][];
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
float win[] = new float[n];
for(int i=0;i<n;i++){
win[i] = sc.nextFloat();
}
int k = sc.nextInt();
dp = new float[n][k+1];
System.out.println(getWins(k,win,0,n-1));
}
static float getWins(int k,float win[],int start,int end){
if(start>end)return 1;
if(dp[start][k]!=0)return dp[start][k];
float include = 0,exclude = 0;
int n=win.length;
if(k==0)return (1-win[start])*getWins(k,win,start+1,end);
include = win[start]*getWins(k-1,win,start+1,end);
if(k <= end-start){
exclude = (1-win[start])*getWins(k,win,start+1,end);
}
dp[start][k] = include+exclude;
return dp[start][k];
}
}