Given n number of rectangles with it’s L-length and B-Breadth. We can turn any rectangle by 90 degrees. In other words, after turning them, the breadth will become length and length will be breadth.
The task is to check if there is a way to make the rectangles go in order of non-ascending breadth. That is, a breadth of every rectangle has to be not greater than the breadth of the previous rectangle.
Note: You can not change the order of the rectangles.
l = 3, b = 4
l1 = 4, b1 = 6
l2 = 3, b2 = 5
The given breadths are [ 4, 6, 5 ] we can rotate the second and the third rectangle so that the breadths will satisfy the above condition [ 4, 4, 3 ] ( 3 is not greater than 4 and 4 is not greater than 4 ) which is why we print YES.
The breadths are [ 60, 55, 80 ] as 55 55 or 56 > 55. So it’s not possible to arrange the breadths in non-ascending order, which is why we’ll print NO.
Approach: Below is the step by step algorithm to solve this problem:
- Initialize n rectangle with their lengths and breadths.
- Iterate over the rectangle from left to right.
- Turn each rectangle in such a way that it’s breadth is as big as possible but not greater than the previous rectangle.
- If on some iteration there is no such way to place the rectangle, the answer is “NO”
Below is the implementation of above approach:
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Rearrange all elements of array which are multiples of x in increasing order
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Check if a number has digits in the given Order
- Check if the characters of a given string are in alphabetical order
- Check if words are sorted according to new order of alphabets
- Sum of Areas of Rectangles possible for an array
- Find if two rectangles overlap
- Number of rectangles in N*M grid
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Total area of two overlapping rectangles
- Count Distinct Rectangles in N*N Chessboard
- Smallest square formed with given rectangles
- Number of rectangles in a circle of radius R
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.