 # FUNHAND Editorial

Setter: Tejas Pandey
Tester: Harris Leung
Editorialist: Jakub Safin, Pratiyush Mishra

Easy

None

# PROBLEM:

Chef has 3 decks consisting of N cards each, numbered from 1 to N. He draws out 1 card from each deck randomly with each card having an equal probability of being drawn.

Chef drew cards numbered A and B from the first 2 decks respectively. Now he wonders what is the probability that he will end up with a funny hand after drawing the third card.

A funny hand is when 3 consecutive numbered cards are present in your hand. Please help Chef calculate the probability of ending up with a funny hand after drawing the last card.

If the final probability of ending up with a funny hand is P. You need to print P⋅N, it can be shown that this value is an integer.

# EXPLANATION:

For a particular test case, three integers N, A and B are taken as inputs. The cards in a deck are numbered from 1 to N and, A, B are the numbers on cards drawn from the first 2 decks.

For possibility of forming a funny hand 3 cases are possible:

• The third drawn card number is the smallest and A, B are consecutive. In this case the third number takes the value which is minimum of A and B minus 1.
• The third drawn card number is the largest and A, B are consecutive. In this case the third number takes the value which is maximum of A and B plus 1.
• The third drawn card number lies between A and B and all three are consecutive.

Also the values on cards are between 1 and N so smallest number in a consecutive sequence can be 1 and the largest being N.

All the above possibilities need to be checked to find the total possible cases for forming a funny hand.

# TIME COMPLEXITY:

O(1) for each test case.

# SOLUTION:

where my code faills ??
https://www.codechef.com/viewsolution/61903381

break for a=1 and b=3,n=6
it will output zero as difference is 2 but we can take third hand as 2 whose probability is (1/6)*6.

I am unable to understand where my code is failing

``````# cook your dish here

t=int(input())

while(t>0):
n,a,b=map(int,input().split())

if(abs(a-b)==2 or abs(a-b)==1):
if(a==1 or b==1 or a==n or b==n):
print(1)
else:
print(2)
else:
print(0)

t-=1
``````

for my code also answer 1 is coming ,plzz check again

n = 6, a = 2, b = 4

plzz tell where my code fails ??

n = 5, a = 4, b = 3, line 18

Can anyone please help me in which case does my code fail, i have tried with custom input but can’t find any case:
https://www.codechef.com/viewsolution/62041938

Try:

2
4 2 3
4 3 2

i guess for everyone who’s getting it wrong it’s failing at the testcase of a==b which is printing 1 which has to print 0 1 Like

thanks brother i was doing mistake in that edge case

thanks bro💗

thanks 