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
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Color all boxes in line such that every M consecutive boxes are unique
- Check whether it is possible to join two points given on circle such that distance between them is k
- Check if a circle lies inside another circle or not
- Equation of circle when three points on the circle are given
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Area of the circle that has a square and a circle inscribed in it
- Number of visible boxes after putting one inside another
- Total number of different staircase that can made from N boxes
- Count the number of ways to fill K boxes with N distinct items
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Check if any point overlaps the given Circle and Rectangle
- Minimum LCM and GCD possible among all possible sub-arrays
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Queries to check whether bitwise AND of a subarray is even or odd
- Check if possible to move from given coordinate to desired coordinate
- Check if it is possible to reach vector B by rotating vector A and adding vector C to it
- Check if it is possible to create a polygon with a given angle
Improved By : chitranayal