Probability of a key K present in array | C # code to find the probability of search key K present in array ; Function to find the probability ; find probability ; Driver Code
using System ; class GFG { static float kPresentProbability ( int [ ] a , int n , int k ) { float count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == k ) count ++ ; return count / n ; } 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 ) ; } }