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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of Areas of Rectangles possible for an array
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Rearrange all elements of array which are multiples of x in increasing order
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Rearrange array such that difference of adjacent elements is in descending order
- Rearrange array to obtain maximum possible value of concatenation of prefix GCDs
- Find if two rectangles overlap
- Number of unique rectangles formed using N unit squares
- Number of rectangles in N*M grid
- Total area of two overlapping rectangles
- Number of rectangles in a circle of radius R
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Smallest square formed with given rectangles
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Find the minimum number of rectangles left after inserting one into another
- Count of distinct rectangles inscribed in an equilateral triangle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.