Probability of collision between two trucks
Last Updated :
20 Oct, 2021
Given two strings S and T, where S represents the first lane in which vehicles move from left to right and T represents the second lane in which vehicles move from right to left. Vehicles can be either B (bike), C (car), or T (truck). The task is to find the probability of collision between two trucks.
Examples:
Input: S = “TCCBCTTB”, T = “BTCCBBTT”
Output: 0.194444
Explanation:
Total collision = 7
Total accident = 36
Therefore, the probability can be calculated by 7/36 = 0.19444.
Input: S = “BTT”, T = “BTCBT”
Output: 0.25000
Illustration:
S = "TCCBCTTB", T = "BTCCBBTT"
Possible cases | Accidents | Collision
-----------------------------------------
TCCBCTTB | |
BTCCBBTT | 8 | 1
| |
TCCBCTTB | |
BTCCBBTT | 7 | 3
| |
TCCBCTTB | |
BTCCBBTT | 6 | 1
| |
TCCBCTTB | |
BTCCBBTT | 5 | 0
| |
TCCBCTTB | |
BTCCBBTT | 4 | 0
| |
TCCBCTTB | |
BTCCBBTT | 3 | 0
| |
TCCBCTTB | |
BTCCBBTT | 2 | 1
| |
TCCBCTTB | |
BTCCBBTT | 1 | 1
Total number of accidents: 8+7+6+5+4+3+2+1=36
Total number of collision: 1+3+1+0+0+0+1+1=7
Probability: 7/36=0.19444
Approach: Follow the steps below to solve the problem:
- Find the total number of favorable outcomes as the total number of collisions(accidents between trucks) and the total number of possible outcomes(total number of collisions) as the total number of accidents.
- Initialize a variable answer equals to 0 to stores the count of collisions.
- Count the number of trucks in the string T and store it in a variable count.
- Iterate over the characters of the strings S and T simultaneously:
- If S[i] is equal to ‘T’, increment answer by count.
- If T[i] is equal to ‘T’, decrement the count by 1.
- Now, calculate the total number of possible outcomes (total number of accidents). It is the sum of all the length of overlapping if keep shifting string a towards right or string b towards left by one unit.
- Let the length of string be N and string b be M. Then, the total number of overlapping will be:
- Find the probability as the ratio of the count of the collisions and the count of the accidents.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double count_of_accident(string a,
string b)
{
int n = a.size(), m = b.size();
if (n > m)
return (m * (m + 1)) / 2;
else
return (n * (n + 1))
/ 2
+ (m - n) * n;
}
double count_of_collision(string a,
string b)
{
int n = a.size(), m = b.size();
int answer = 0;
int count_of_truck_in_lane_b = 0;
for ( int i = 0; i < m; i++)
if (b[i] == 'T' )
count_of_truck_in_lane_b++;
for ( int i = 0; i < n && i < m; i++) {
if (a[i] == 'T' )
answer
+= count_of_truck_in_lane_b;
if (b[i] == 'T' )
count_of_truck_in_lane_b--;
}
return answer;
}
double findProbability(string a,
string b)
{
double total_outcome
= count_of_accident(a, b);
double favourable_outcome
= count_of_collision(a, b);
cout << favourable_outcome
/ total_outcome;
}
int main()
{
string S = "TCCBCTTB" , T = "BTCCBBTT" ;
findProbability(S, T);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int count_of_accident(String a,
String b)
{
int n = a.length(), m = b.length();
if (n > m)
return (m * (m + 1 )) / 2 ;
else
return (n * (n + 1 ))
/ 2
+ (m - n) * n;
}
static double count_of_collision(String a,
String b)
{
int n = a.length(), m = b.length();
double answer = 0 ;
int count_of_truck_in_lane_b = 0 ;
for ( int i = 0 ; i < m; i++)
if (b.charAt(i) == 'T' )
count_of_truck_in_lane_b++;
for ( int i = 0 ; i < n && i < m; i++)
{
if (a.charAt(i) == 'T' )
answer
+= count_of_truck_in_lane_b;
if (b.charAt(i) == 'T' )
count_of_truck_in_lane_b--;
}
return answer;
}
static void findProbability(String a,
String b)
{
int total_outcome
= count_of_accident(a, b);
double favourable_outcome
= count_of_collision(a, b);
System.out.printf( "%4f" ,favourable_outcome
/ total_outcome);
}
public static void main(String[] args)
{
String S = "TCCBCTTB" , T = "BTCCBBTT" ;
findProbability(S, T);
}
}
|
Python3
def count_of_accident(a, b):
n = len (a)
m = len (b)
if (n > m):
return (m * (m + 1 )) / 2
else :
return ((n * (n + 1 )) / 2 +
(m - n) * n)
def count_of_collision(a, b):
n = len (a)
m = len (b)
answer = 0
count_of_truck_in_lane_b = 0
for i in range ( 0 , m):
if (b[i] = = 'T' ):
count_of_truck_in_lane_b + = 1
i = 0
while (i < m and i < n):
if (a[i] = = 'T' ):
answer + = count_of_truck_in_lane_b
if (b[i] = = 'T' ):
count_of_truck_in_lane_b - = 1
i + = 1
return answer
def findProbability(a, b):
total_outcome = count_of_accident(a, b);
favourable_outcome = count_of_collision(a, b);
print (favourable_outcome / total_outcome)
if __name__ = = "__main__" :
S = "TCCBCTTB"
T = "BTCCBBTT"
findProbability(S, T)
|
C#
using System;
class GFG
{
static int count_of_accident(String a,
String b)
{
int n = a.Length, m = b.Length;
if (n > m)
return (m * (m + 1)) / 2;
else
return (n * (n + 1))
/ 2
+ (m - n) * n;
}
static double count_of_collision(String a,
String b)
{
int n = a.Length, m = b.Length;
double answer = 0;
int count_of_truck_in_lane_b = 0;
for ( int i = 0; i < m; i++)
if (b[i] == 'T' )
count_of_truck_in_lane_b++;
for ( int i = 0; i < n && i < m; i++)
{
if (a[i] == 'T' )
answer
+= count_of_truck_in_lane_b;
if (b[i] == 'T' )
count_of_truck_in_lane_b--;
}
return answer;
}
static void findProbability(String a,
String b)
{
int total_outcome
= count_of_accident(a, b);
double favourable_outcome
= count_of_collision(a, b);
Console.Write( "{0:F4}" , favourable_outcome
/ total_outcome);
}
public static void Main(String[] args)
{
String S = "TCCBCTTB" , T = "BTCCBBTT" ;
findProbability(S, T);
}
}
|
Javascript
<script>
function count_of_accident(a, b)
{
let n = a.length, m = b.length;
if (n > m)
return (m * (m + 1)) / 2;
else
return (n * (n + 1))
/ 2
+ (m - n) * n;
}
function count_of_collision(a, b)
{
let n = a.length, m = b.length;
let answer = 0;
let count_of_truck_in_lane_b = 0;
for (let i = 0; i < m; i++)
if (b[i] == 'T' )
count_of_truck_in_lane_b++;
for (let i = 0; i < n && i < m; i++)
{
if (a[i] == 'T' )
answer
+= count_of_truck_in_lane_b;
if (b[i] == 'T' )
count_of_truck_in_lane_b--;
}
return answer;
}
function findProbability(a, b)
{
let total_outcome
= count_of_accident(a, b);
let favourable_outcome
= count_of_collision(a, b);
document.write( favourable_outcome
/ total_outcome);
}
let S = "TCCBCTTB" , T = "BTCCBBTT" ;
findProbability(S, T);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...