Probability of finding an element K in a Singly Linked List
Last Updated :
26 Feb, 2023
Given a Singly Linked List of size N and another key K, we have to find the probability that the key K is present in the Singly Linked List.
Examples:
Input: Linked list = 2 -> 3 -> 3 -> 3 -> 4 -> 2, Key = 5
Output: 0
Explanation:
Since the value of Key is 5 which is not present in List, the probability of finding the Key in the Linked List is 0.
Input: Linked list = 2 -> 3 -> 5 -> 1 -> 9 -> 8 -> 0 -> 7 -> 6 -> 5, Key = 5
Output: 0.2
Approach:
The probability of finding a Key element K in a Singly Linked List is given below:
Probability = Number of Occurrences of Element K / Size of the Linked List
In our approach, we will first count the number of Element K present in the Singly Linked List and then the probability will be calculated by dividing the number of occurrences of K by the size of the Singly Linked List.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* push(Node* head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = head_ref;
head_ref = new_node;
return head_ref;
}
int getCount(Node* head) {
int count = 0;
Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
float kPresentProbability(Node* head, int n, int k) {
float count = 0;
Node* current = head;
while (current != NULL) {
if (current->data == k)
count++;
current = current->next;
}
return count / n;
}
int main() {
Node* head = NULL;
head = push(head, 2);
head = push(head, 3);
head = push(head, 5);
head = push(head, 1);
head = push(head, 9);
head = push(head, 8);
head = push(head, 0);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
cout << kPresentProbability(head, getCount(head), 5);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc (
sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int getCount( struct Node* head)
{
int count = 0;
struct Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
float kPresentProbability(
struct Node* head,
int n, int k)
{
float count = 0;
struct Node* current = head;
while (current != NULL) {
if (current->data == k)
count++;
current = current->next;
}
return count / n;
}
int main()
{
struct Node* head = NULL;
push(&head, 2);
push(&head, 3);
push(&head, 5);
push(&head, 1);
push(&head, 9);
push(&head, 8);
push(&head, 0);
push(&head, 7);
push(&head, 6);
push(&head, 5);
printf ( "%.1f" ,
kPresentProbability(
head, getCount(head), 5));
return 0;
}
|
Java
class GFG{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int getCount(Node head)
{
int count = 0 ;
Node current = head;
while (current != null )
{
count++;
current = current.next;
}
return count;
}
static float kPresentProbability(Node head,
int n, int k)
{
float count = 0 ;
Node current = head;
while (current != null )
{
if (current.data == k)
count++;
current = current.next;
}
return count / n;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 2 );
head = push(head, 3 );
head = push(head, 5 );
head = push(head, 1 );
head = push(head, 9 );
head = push(head, 8 );
head = push(head, 0 );
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 5 );
System.out.printf( "%.1f" , kPresentProbability(
head, getCount(head), 5 ));
}
}
|
Python3
class Node:
def __init__( self , data, next = None ):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , data):
new_node = Node(data)
new_node. next = self .head
self .head = new_node
def getCount( self ):
current = self .head
count = 0
while current is not None :
count + = 1
current = current. next
return count
def kPresentProbability( self , n, k):
current = self .head
count = 0.0
while current is not None :
if current.data = = k:
count + = 1
current = current. next
return count / n
if __name__ = = "__main__" :
llist = LinkedList()
llist.push( 2 )
llist.push( 3 )
llist.push( 5 )
llist.push( 1 )
llist.push( 9 )
llist.push( 8 )
llist.push( 0 )
llist.push( 7 )
llist.push( 6 )
llist.push( 5 )
print (llist.kPresentProbability(
llist.getCount(), 5 ))
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int getCount(Node head)
{
int count = 0;
Node current = head;
while (current != null )
{
count++;
current = current.next;
}
return count;
}
static float kPresentProbability(Node head,
int n, int k)
{
float count = 0;
Node current = head;
while (current != null )
{
if (current.data == k)
count++;
current = current.next;
}
return count / n;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 2);
head = push(head, 3);
head = push(head, 5);
head = push(head, 1);
head = push(head, 9);
head = push(head, 8);
head = push(head, 0);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
Console.Write( "{0:F1}" , kPresentProbability(
head, getCount(head), 5));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function getCount(head) {
var count = 0;
var current = head;
while (current != null ) {
count++;
current = current.next;
}
return count;
}
function kPresentProbability(head, n, k) {
var count = 0;
var current = head;
while (current != null ) {
if (current.data == k) count++;
current = current.next;
}
return count / n;
}
var head = null ;
head = push(head, 2);
head = push(head, 3);
head = push(head, 5);
head = push(head, 1);
head = push(head, 9);
head = push(head, 8);
head = push(head, 0);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
document.write(kPresentProbability(head, getCount(head), 5).toFixed(1));
</script>
|
Time Complexity: O(n) where n is number of nodes.
Space Complexity: O(n) as linked list is created, where n is number of nodes.
Share your thoughts in the comments
Please Login to comment...