Contents
Single Lane Highway Codevita 9 Solution
Certain number of cars are passing a single lane road. Speeds of all cars vary. It is easy to see, that depending on the speeds of the cars various groups will be formed.
Being a single lane road passing/overtaking is not allowed. Given speeds of cars, calculate how many groups can be formed if all possible permutations are taken into account. Refer example1 for better understanding.
Print number of groups divided by the number of permutations.
Constraints
0 <= N < 10 ^ 5
0 <= speed of individual vehicle < 10 ^ 9
Input
First line contains an integer N, which denotes the number of vehicles
Second line contains N space separated integers which denotes the speed of individual vehicle.
Output
Print number of groups divided by the number of permutations rounded upto 6 decimal places.
Time Limit
1
Example 1
Input
3
10 20 30
Output
1.833333
Explanation:
So all possible permutations are:
{10 20 30}
{10 30 20}
{20} {10 30}
{20 30} {10}
{30} {10 20}
{30 20} {10}
So here there are total 6 permutations, and total number of groups are 11.
So, output is 11/6 = 1.833333
Example 2
Input
4
56 78 13 92
Output
2.083333
Explanation:
So here there are total 24 permutations,
For example:
{56 78 13 92}
{92} {13 78 56}
{56} {13 92 78}
{78 92} {13 56}
So on and so forth. The total number of groups are 50.
So, the output is 50/24 = 2.083333″
SOLUTION
Program: Single Lane Highway Codevita 9 Solution in Python
from itertools import permutations
import math
n=int(input())
l=list(map(int,input().split()))
cc=[]
s1=math.factorial(n)//math.factorial(n-(n))
s2=math.factorial(n)//math.factorial(n-(n-1))
cc.append(s1)
cc.append(s2)
if(n%2==0):
t=sum(cc)+2
else:
t=sum(cc)-1
print("%.6f"%(t/cc[-1]))
Program: Single Lane Highway Codevita 9 Solution in C++
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int speed[n];
for (int i = 0; i < n; i++)
cin >> speed[i];
double per = 1, group = 0;
for (int i = 1; i <= n; i++)
{
per *= i;
for (int j = 1, mult = 1; j <= i; mult *= ++j)
{
group += (per / mult);
}
}
if (n % 2 == 0)
group--;
group -= n;
printf("%0.6f", group / per);
}
Codevita Season 9 All Questions Solution
- Even Odd Codevita 9 Solution
- Largest Gold Ingot Codevita 9 Solution
- Fill the Cube Codevita 9 Solution
- Logic for Single Lane Highway Codevita 9
- Faulty Keyboard Codevita 9 Solution 2020
- Signal Connection Codevita 9 Solution 2020
- Closing Value Codevita 9 Solution
- CodeVita season 9 Zone 2 All Solutions
- Railway Station Codevita 9 Solution
- Count Pairs Codevita 9 Solution
- 7 X 7 Codevita 9 Solution
- Tennis Score codevita 9 Solution
- Unlocker Codevita 9 Solution
- Path through graph Codevita 9 Solution
- Secret Word Codevita 9 Solution
- 3 Palindrome Codevita 9 Solution
- Max Sum Codevita 9 Solution
- Equalize Weights Codevita 9 Solution
- Binary Equivalent Codevita 9 Solution
- String Word Codevita 9 Solution
- 4 Particles Codevita 9 Solution
- String Pair Codevita 9 Solution
- Corona Virus Codevita 9 Solutions
- Factor of 3 Codevita 9 Solutions
- Single Lane Highway Codevita 9 Solution