TCS 2020 MOCKVITA

All you have to do is calculate the number of cars which would be present at the origin(0,0) at any time.
for every car, time at which it is at origin is given by : Time^2 = (x^2 + y^2) / (v^2) [since , time = dist/speed]
if the number of cars at any instant is >1 , then there is a collision and the number of collisions at that instant would be nC2 (where n is the number of cars at that instant)
Just take the summation of all the collisions and that would be the answer.

You have to find the distance of every coordinate from the origin and then divide by its speed.Doing this u would get the time in which that particular car will reach origin. Now u have to perform this operation for all the cars i. e all coordinates. And then find out how many cars will reach origin at same time.the number of cars reaching origin at same time would cause collision.

But if we use sqrt then my code is giving wrong answer else if we dont find sqrt and directly work with this then it gives righr answer in c++. Why this happenning?

1 Like

my code has only passed the pretest of this prime fibnacci and if it helps you in anyway here is my code

def fib(n, f, s):
t = 0
for i in range(n - 1):
t = (f + s)
f = s
s = t
return t

def Solve(n1, n2):
primes = []
for i in range(n1, n2 + 1):
flag = 0
for j in range(2, i):
if i % j == 0:
flag = 1
break
if flag == 0:
primes.append(i)
new = []
c = ā€˜ā€™
for i in range(len(primes)):
for j in range(i + 1, len(primes)):
c = str(primes[i]) + str(primes[j])
cc = str(primes[j]) + str(primes[i])
if intĀ© not in new:
new.append(intĀ©)
if int(cc) not in new:
new.append(int(cc))
new_primes = []
for ch in new:
flag2 = 0
for k in range(2, ch):
if ch % k == 0:
flag2 = 1
break
if flag2 == 0:
new_primes.append(ch)
count = len(new_primes)
ans = fib(count - 1, min(new_primes), max(new_primes))
return ans

n1, n2 = map(int, input().split())
print(Solve(n1, n2))

same issue encountered.

the only thing you need to is instead inbuilt sqrt() function your time variable should be initialize with d/(v*v)

Its because when we find sqrt and then divide it by time, the precision changes for some of the cars and so we get different times for the same set of cars.

Can you explain why to use d/v*v and how is it equivalent to sqrt(d)/v ?

Hereā€™s my solution for prime fibonacci.

#include
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
// your code goes here
int i,j,n,sp=0;
ll n1,n2,tmp;
cin>>n1>>n2;
int arr[9999]={0};
vector primes;
vector combinations;
vector presence(9999,0);
for(i=2;i<=9999;i++)
{
if(arr[i])
continue;
else
{
if(i>=n1 && i<=n2)
primes.push_back(i);
for(j=i*2;j<=9999;j+=i)
{
arr[j]=1;
}
}
}
n=primes.size();
n1=9999;
n2=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
continue;
if(primes[j]<10)
{
tmp=primes[i]*10+primes[j];

      }
      else
      {
          tmp=primes[i]*100+primes[j];
      }
        combinations.push_back(tmp);
        if(arr[tmp]==0 && presence[tmp]==0)
        {
	        if(tmp>n2)
	        n2=tmp;
	        if(tmp<n1)
	        n1=tmp;
	        sp++;
	        presence[tmp]=1;
        }
      
  }

}
for(i=3;i<=sp;i++)
{
tmp=n1+n2;
n1=n2;
n2=tmp;
}
cout<<tmp;

return 0;
}

Can anyone telll me what is wrong with this code?
> #include
> #include<bits/stdc++.h>
> #include<unordered_map>
> using namespace std;
> typedef long long ll;
> int main() {
> // your code goes here
> int c,i;
> cin>>c;
> float distance;
> unordered_map<double,int> dist;
> ll x,y,s,ans=0;
> for(i=0;i<c;i++)
> {
> cin>>x>>y>>s;
> distance=sqrt(xx+yy)/s;
> dist[distance]++;
> }
> unordered_map<double,int>::iterator p;
> for(p=dist.begin();p!=dist.end();p++)
> {
> ans+=(p->second*(p->second-1))/2;
> }
> cout<<ans;
> return 0;
> }

But for finding the distance between a point and origin d=sqrt(x^2+ y^2)
And if we dont square root it and divide it by v^2 the answer will be time^2. How is that correct??

from itertools import permutations
a,b=map(int,input().split())
l=[]
for j in range(a,b+1):
p=0
for k in range(2,j):
if(j%k==0):
p=1
break
else:
pass
if(p==0):
l.append(str(j))
#print(l)
l=set(l)
#print
lx=[]
lp=permutations(l, 2)
for e in lp:
z="".join(e)
lx.append(z)
#print(lx)
l2=[]
#print(len(lx))
lx=list(set(lx))
#print(len(lx))
for e in lx:
e=int(e)
p=0
for k in range(2,e):
if(e%k==0):
p=1
break
else:
pass
if(p==0):
l2.append(e)
#print(l2)
#print(len(l2))
l1=sorted(l2)
a=l1[0]

b=l1[-1]
#print(a,b)
n=len(l1)
#print(n)
for i in range(3,n+1):
s=a+b
a=b
b=s
print(b)

Idont know why Iam wrong please help meā€¦
2nd ques of mock vita

I did the same thing. I got wrong answer. Could you tell me what iā€™m doing wrong?

#define ll long long
int main()
{
ll int c;
cin>>c;
vector < double > timestamps;
unordered_map<double,double> mymap;
while(cā€“)
{
double x, y, speed;
cin>>x>>y>>speed;
double dist,time;
dist = sqrt((xx)+(yy));
time = dist/speed;
timestamps.push_back(time);
}

for(double x : timestamps)
mymap[x]++;
long long int cnt = 0;

for(auto it = mymap.begin();it != mymap.end();it++)
{

  long long int n = it->second;
  cnt = cnt+((n*(n-1))/2);

}

cout<<cnt;
}`

Thanks broā€¦ Can you please explain how did you arrive at this??

Donā€™t use sqrt function. Juat do as mentioned above. Sqrt function is causing problem here

hey, could you explain why did you go ahead with n (n-1) / 2. I went ahead with nc2 since you have to chose any two accident from n accidents. Well it didnā€™t pass the private test cases but still.

In your case accident of A with Band B with A will be considered as a saperate while it should be only one. So nc2 will fail.


can anybody please help me with this problem .
it would be great !!

1 Like

If one car reaches origin at the same time then 0 collisions.
If two cars reache origin at the same time then 1 collisions.
If three cars reach origin at the same time then 3 collisions.
If four cars reach origin at the same time then 6 collisions.
ā€¦ and so on.
Which is nothing but n*(n-1)/2

1 Like

I tried the questions, Not sure if they are 100% correct or not
you can go and check my solutions here: GitHub - PritimoySarkar/Mockvita-2020-Soultion: Problem and solutions of Mockvita 1 (2020)