Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

The Cat and Mouse Juxtaposition

By RHaque · Limits 1s, 512 MB

In a house, there are nn rooms in a line, with the rooms being numbered from 11 to nn from the left to the right. In each room, there are two doors, allowing someone to move from his current room to the room to his left or to the room his right. Two exceptions to this are the rooms numbered 11 and nn, which only have doors to their right and left respectively since there’s no room to the left of room 11, and no room to the right of room nn.

In the house, there is a cat named Tom, and a mouse named Jerry. Tom is initially at room aa, while Jerry is initially at room bb. However, Tom does not know where Jerry is, and Jerry does not know where Tom is. As usual, Tom wants to catch Jerry. At each minute, he uses one of the doors to move to another room. Jerry also keeps moving from one room to another. However, instead of using the doors, Jerry has dug small underground tunnels that allow him to move between consecutive rooms without being noticed.

The following figure shows the layout of the house when there are 55 rooms.

It is guaranteed that during each minute, both Tom and Jerry will always move to an adjacent room and will never stay in the same room for more than one minute. The only way for Tom to catch Jerry is if they both find themselves in the same room at the exact same time. Both of them will run blindly, that is, unless they are at the same room, neither will no where the other one is.

Given the values of aa and bb, find out if there is any chance that Tom will catch Jerry.


The input consists of a single line containing three integers - nn, aa and bb, denoting the total number of rooms and the rooms in which Tom and Jerry are initially.


1a,bn1051 \leq a, b \leq n \leq 10^5


If there is any chance that Tom will catch Jerry, output “YES” (without the quotes). Otherwise, output “NO” (without the quotes).


4 3 3

Since Tom and Jerry are both in the same room initially, Tom will catch Jerry right away.

5 2 4

In minute 1, Tom is in room 2 and Jerry is in room 4. If Tom moves to the right and Jerry moves to the left in the next minute, they will both move to room 3, where Tom will capture Jerry.



    85% Solution Ratio

    TanbeerEarliest, 2w ago

    AbinRoyFastest, 0.0s

    SHAHIN98Lightest, 0 B

    bokaifShortest, 60B


    Login to submit