Consider n boxes numbered arranged in a circle in increasing order in clockwise direction (numbered from 1 to n). You are given q queries, each containing two integer i and j. The task is to check whether it is possible to connect box i to box j by a rod without intersecting rods used to join other boxes in previous queries. Also, every box can be connected to at most one other box and no box can be connected to itself.
Input: n = 10, q = 7
q1 = (1, 5)
q2 = (2, 7)
q3 = (2, 3)
q4 = (2, 4)
q5 = (9, 9)
q6 = (10, 9)
q7 = (8, 6)
Box 1 and v 5 can be connected by a rod.
Box 2 and Box 7 cannot be connected by a rod because this rod intersects with the rod connecting Box 1 and Box 5.
Box 2 and Box 3 can be connected without criss-cross.
Box 2 and Box 4 cannot be connected as Box 2 is already connected to Box 3.
Box 9 and Box 9 cannot be connected as no box can be connected to itself.
Box 10 and Box 9 can be connected.
Box 8 and Box 6 can be connected.
Suppose box x is already connected to box y. And we need to connect box i to box j.
Now, observe there can be two cases where two rods connecting box i and box j will intersect with rod connecting box x and box y.
Case 1: x < i and y lies between i and j:
Case 2: x lies between i and j and y >j:
We will explicitly check the case of whether a rod is intendes to connect to itself or if the rod intends to connect two boxes such that at least one of them is already connected.
Hence, we will check the above two condition. If any of two meet, it is not possible to connect else we can connect the boxes.
Below is the implementation of the above approach:
YES NO YES NO NO YES YES
- Check whether it is possible to join two points given on circle such that distance between them is k
- Color all boxes in line such that every M consecutive boxes are unique
- Check if a circle lies inside another circle or not
- Queries on count of points lie inside a circle
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Check if a line touches or intersects a circle
- Check whether a point exists in circle sector or not.
- Check whether given circle resides in boundary maintained by two other circles
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Equation of circle when three points on the circle are given
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Number of visible boxes after putting one inside another
- Total number of different staircase that can made from N boxes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : chitranayal