Probability of a key K present in array

• Difficulty Level : Easy
• Last Updated : 10 Mar, 2021

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 = 3
Output :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 = 5
Output :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 using namespace std; // Function to find the probabilityfloat 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 Codeint main(){     int A[] = { 4, 7, 2, 0, 8, 7, 5 };    int K = 3;    int N = sizeof(A) / sizeof(A);    cout << kPresentProbability(A, N, K);    return 0;}

Python3

 # Python3 code to find the# probability of search key# K present in 1D-array (list). # Function to find the probabilitydef kPresentProbability(a, n, k) :     count = a.count(k)     # find probability upto    # 2 decimal places    return round(count / n , 2) # Driver Codeif __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

Java

 // Java code to find the probability// of search key K present in arrayclass GFG{ // Function to find the probabilitystatic 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 Codepublic 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

C#

 // C# code to find the probability// of search key K present in arrayusing System; class GFG{ // Function to find the probabilitystatic 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 Codepublic 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



Javascript


Output
0

Time Complexity: O(N)

My Personal Notes arrow_drop_up