# Probability of a key K present in array

Given an array A[] and size of array is N and one another key K. The task is to find the probability that the Key K present in array.

**Examples:**

Input :N = 6 A[] = { 4, 7, 2, 0, 8, 7, 5 } K = 3Output :0 Since value of k = 3 is not present in array, hence the probability of 0.Input :N = 10 A[] = { 2, 3, 5, 1, 9, 8, 0, 7, 6, 5 } K = 5Output :0.2

The probability of can be found out using the below formula:

Probability = total number of K present / size of array.

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

Below is the implementation of the above approach:

## C++

`// C++ code to find the probability of ` `// search key K present in array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the probability ` `float` `kPresentProbability(` `int` `a[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `float` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(a[i] == k) ` ` ` `count++; ` ` ` ` ` `// find probability ` ` ` `return` `count / n; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `A[] = { 4, 7, 2, 0, 8, 7, 5 }; ` ` ` `int` `K = 3; ` ` ` `int` `N = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` `cout << kPresentProbability(A, N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find the ` `# probability of search key ` `# K present in 1D-array (list). ` ` ` `# Function to find the probability ` `def` `kPresentProbability(a, n, k) : ` ` ` ` ` `count ` `=` `a.count(k) ` ` ` ` ` `# find probability upto ` ` ` `# 2 decimal places ` ` ` `return` `round` `(count ` `/` `n , ` `2` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `A ` `=` `[ ` `4` `, ` `7` `, ` `2` `, ` `0` `, ` `8` `, ` `7` `, ` `5` `] ` ` ` `K ` `=` `2` ` ` `N ` `=` `len` `(A) ` ` ` ` ` `print` `(kPresentProbability( A, N, K)) ` ` ` `# This code is contributed ` `# by AnkitRai1 ` |

*chevron_right*

*filter_none*

## Java

`// Java code to find the probability ` `// of search key K present in array ` `class` `GFG ` `{ ` ` ` `// Function to find the probability ` `static` `float` `kPresentProbability(` `int` `a[], ` ` ` `int` `n, ` ` ` `int` `k) ` `{ ` ` ` `float` `count = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `if` `(a[i] == k) ` ` ` `count++; ` ` ` ` ` `// find probability ` ` ` `return` `count/ n; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `A[] = { ` `4` `, ` `7` `, ` `2` `, ` `0` `, ` `8` `, ` `7` `, ` `5` `}; ` ` ` `int` `K = ` `2` `; ` ` ` `int` `N = A.length; ` ` ` `double` `n = kPresentProbability(A, N, K); ` ` ` `double` `p = (` `double` `)Math.round(n * ` `100` `) / ` `100` `; ` ` ` `System.out.println(p); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# code to find the probability ` `// of search key K present in array ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the probability ` `static` `float` `kPresentProbability(` `int` `[] a, ` ` ` `int` `n, ` ` ` `int` `k) ` `{ ` ` ` `float` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(a[i] == k) ` ` ` `count++; ` ` ` ` ` `// find probability ` ` ` `return` `count/ n; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[] A = { 4, 7, 2, 0, 8, 7, 5 }; ` ` ` `int` `K = 2; ` ` ` `int` `N = A.Length; ` ` ` `double` `n = kPresentProbability(A, N, K); ` ` ` `double` `p = (` `double` `)Math.Round(n * 100) / 100; ` ` ` `Console.Write(p); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to find the probability ` `// of search key K present in array ` ` ` `// Function to find the probability ` `function` `kPresentProbability(&` `$a` `, ` `$n` `, ` `$k` `) ` `{ ` ` ` `$count` `= 0; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$a` `[` `$i` `] == ` `$k` `) ` ` ` `$count` `++; ` ` ` ` ` `// find probability ` ` ` `return` `$count` `/ ` `$n` `; ` `} ` ` ` `// Driver Code ` `$A` `= ` `array` `( 4, 7, 2, 0, 8, 7, 5 ); ` `$K` `= 2; ` `$N` `= sizeof(` `$A` `); ` `echo` `round` `(kPresentProbability(` `$A` `, ` `$N` `, ` `$K` `), 2); ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.14

**Time Complexity: O(N)**

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Count of anagrams of each string in an array present in another array
- First string from the given array whose reverse is also present in the same array
- Probability of choosing a random pair with maximum sum in an array
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Count of elements A[i] such that A[i] + 1 is also present in the Array
- Count the factors of K present in the given Array
- Sum of all palindrome numbers present in an Array
- Sum of all mersenne numbers present in an array
- Sum of all perfect numbers present in an array
- Count elements present in first array but not in second
- Find elements which are present in first array and not in second
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Sum of elements whose square root is present in the array
- Program to find if two numbers and their AM and HM are present in an array using STL
- Check if a key is present in every segment of size k in an array
- Sum of element whose prime factors are present in array
- Check whether K times of a element is present in array
- Smallest element greater than X not present in the array
- Count pairs with average present in the same array

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.