Shocked !!

Can any one tell what’s wrong in code which is very short and simple.
passing test case but overall wrong answer.

problem link:-link text

my solution:- link text

I cant believe it getting wrong for 3 times for such a basic level problem. Plz I want to know what part in code is producing wrong answer.

thanks

check for this test case, ur answer is “Draw” but actual answer will be Kefa

1
0 10 6 4 3  

edit:-- line no 41 u r making error

if(cc==ck)
printf("Draw\n");  

**EDIT:- ** Bro after seeing ur below answer, yes ur logic seems to be almost correct but a small mistake…
check ur else part when counter1

  else    // both take same steps
 {

  if (vel1 greater than vel2)
 person1 reaches first

  else

  person2 reaches first

 }

ok so in above given example 0 10 6 4 3
i am assuming counter in seconds

counter1 will be 1sec (6/4) and remainder is 2 more distance to cover as (6-0)%4
counter2 will be 1sec (4/3) and remainder is 1 (1 more distance to cover) as (10-6)%3

**So according to else part
counter1==counter2 //true No issue here

then chef have to cover remaining distance of 2 meter where his speed is 4 meter/sec so it will take extra 2/4= .5 sec
and kefa have to cover remaining distance of 1 meter where his speed is 3 meter/sec** which will take nearly 1/3 = .33 sec
so kefa will reach early

so Kefa should be actual answer…

but according to ur logic, whose velocity is higher will reach first…

velocity of chef is higher than kefa, chef will take less time… that’s totally wrong logic

Hint to solve above problem:–
u have to find time for each but for calculation of time u should avoid division method

if ur issue resolved accept this answer and close thread otherwise ping me

Happy Coding
:slight_smile:

1 Like

I have updated my solution link still getting WA. Now can u plz find an logical error or test case for which it fails

You have over complicated the solution :
let me try to show you your mistakes in your code…
Here is the problem…

  
if(cc==ck){  
      if(vc>vk)  
      printf("Chef\n");  
      else if(vk>vc)
      printf("Kefa\n");
      else
      printf("Draw\n");
    }
}

if cc==ck the it doesn’t mean that whose velocity is less will reach first…
#counter example…
#1
#1 11 4 2 4
#here inputs are x1 x2 x3 v1 v2
hence
x1<x3<x2
as per question
#here chef will take
(4-1)/2 = 3/2 = 1.5 secs
#while kefa will take
(11-4)/4 =7/4 = 1.75 secs
#so chef wins…
#but as per your solution kefa wins…

I have seen tons of accepted solutions all have used this logic:

suppose distance between bottle and restaurant is 10 and velocity of guy is 6
then time required will be 10/6.

while i have make use of counter i.e in how many step will guy reach to the bottle from his restuarent .

let say dis=10;

    vel=6;

   counter=0

 while(dis greater than 0)

{

 dis=dis-vel;

 counter++;

}

Now based on this counter one can identify who will take more steps to reach to bottle.

    if(counter1 less than counter2)

    person1 reaches earlier then person2


   else if (counter2 less than counter1)

    person2 reaches earlier then person2


       else    // both take same steps
     {

      if (vel1 greater than vel2)
     person1 reaches first

      else

      person2 reaches first

     }

Now whats wrong in this logic can any one plz tell why im getting WA.Problem is very basic

my code gives Kefa. btw from problem constraints 1 8 11 4 2 doesn’t satisfy as kefas position should be greater then bottle and chefs position.

1 Like

actually it has very simple solution… no loops nothing…

#we should compare their time to get soln…
#time= distance/velocity
so
#let tc be time taken by chef and tk by kefa
hence
#tc is to be compared with tk

tc=dc/vc;

tk=dk/vk;

but then you may fall in problem with float values
hence let u check the sign of tc-tk

let temp = tc-tk…

hence temp = (dc/vc)-(dk/vk)…

#but this may lead to wrong answer due to float…
hence lets find

temp2 =temp*vc*vk = (dc*vk) - (dk*vc);

Now you may check that sign of temp is same as sign of temp 2…
hence finding sign of temp2 will also do…

hence the simple solution is

temp=(dc*vk)-(dk*vc);

if(temp>0)
chef
else if (temp<0)
kefa
else
draw

#note : take all data members in long long it to avoid wrong answer due to overflow…

one min…

now check my answer @code_man

wait again u made mistake while dividing with velocity.

v1 is chefs velocity

v2 is kefas velocity

Are u in hurry bro:)

there is no step concept in this question… t is not integer… it is a float… chef and kefa keeps running even when t=1.00001 ,1.00002 …
hence comparing velocity when steps are same doesn’t seem correct…
example test case:
1 11 4 2 4
here steps of both will be 2… but when you do calculations on paper then chef wins…
but according to your code kefa wins…

1 Like

okay now ?? answer was correct just forgot to change previous step :slight_smile:

HERE is my soln link in case you need reference…

#alternate soln is edit cc==ck part in your code as…
temp=(ck*vc)-(cc*vk);
#temp>0 -> chef
#temp<0 -> kefa
#temp==0 -> draw
#remember taking long long int

you can accept my answer if it was appropriate…

Or do let me know what was inappropriate or what u didn’t liked… just for improving my communication and explaination skills…

Thanks…

PS: I do worry about my accept rate. As well as skills…

answer updated, check it again

@code_man Bro, my above answer is now updated, have a look