Open In App

Check if it is possible to rearrange rectangles in a non-ascending order of breadths

Improve
Improve
Like Article
Like
Save
Share
Report

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.
Examples: 
 

Input:
l = 3, b = 4 
l1 = 4, b1 = 6 
l2 = 3, b2 = 5 
Output: YES 
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. 
Input:
1 60 
70 55 
56 80 
Output: NO 
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: 
 

  1. Initialize n rectangle with their lengths and breadths.
  2. Iterate over the rectangle from left to right.
  3. Turn each rectangle in such a way that it’s breadth is as big as possible but not greater than the previous rectangle.
  4. If on some iteration there is no such way to place the rectangle, the answer is “NO”

Below is the implementation of above approach: 
 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if it possible to form
// rectangles with heights as non-ascending
int rotateRec(int n, int L[], int B[])
{
 
    // set maximum
    int m = INT_MAX;
 
    for (int i = 0; i < n; i++) {
        // replace the maximum with previous maximum
        if (max(L[i], B[i]) <= m)
            m = max(L[i], B[i]);
 
        // replace the minimum with previous minimum
        else if (min(L[i], B[i]) <= m)
            m = min(L[i], B[i]);
 
        // print NO if the above
        // two conditions fail at least once
        else {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
int main()
{
    // initialize the number of rectangles
    int n = 3;
 
    // initialize n rectangles with length and breadth
    int L[] = { 5, 5, 6 };
    int B[] = { 6, 7, 8 };
    rotateRec(n, L, B) == 1 ? cout << "YES"
                            : cout << "NO";
 
    return 0;
}


Java




// Java implementation of above approach
import java.io.*;
 
class GFG {
    
// Function to check if it possible to form
// rectangles with heights as non-ascending
 static int rotateRec(int n, int L[], int B[])
{
 
    // set maximum
    int m = Integer.MAX_VALUE;
 
    for (int i = 0; i < n; i++) {
        // replace the maximum with previous maximum
        if (Math.max(L[i], B[i]) <= m)
            m = Math.max(L[i], B[i]);
 
        // replace the minimum with previous minimum
        else if (Math.min(L[i], B[i]) <= m)
            m = Math.min(L[i], B[i]);
 
        // print NO if the above
        // two conditions fail at least once
        else {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
 
    public static void main (String[] args) {
    // initialize the number of rectangles
    int n = 3;
 
    // initialize n rectangles with length and breadth
    int L[] = { 5, 5, 6 };
    int B[] = { 6, 7, 8 };
    if(rotateRec(n, L, B) == 1)
     System.out.println( "YES");
     else
     System.out.println( "NO");
    }
}
 // This Code is contributed by inder_verma..


Python3




# Python3 implementation of above approach
import sys;
 
# Function to check if it possible
# to form rectangles with heights
# as non-ascending
def rotateRec(n, L, B):
 
    # set maximum
    m = sys.maxsize;
 
    for i in range(n):
 
        # replace the maximum with
        # previous maximum
        if (max(L[i], B[i]) <= m):
            m = max(L[i], B[i]);
 
        # replace the minimum
        # with previous minimum
        elif (min(L[i], B[i]) <= m):
            m = min(L[i], B[i]);
 
        # print NO if the above two
        # conditions fail at least once
        else:
            return 0;
     
    return 1;
 
# Driver code
 
# initialize the number
# of rectangles
n = 3;
 
# initialize n rectangles
# with length and breadth
L = [5, 5, 6];
B = [ 6, 7, 8 ];
if(rotateRec(n, L, B) == 1):
    print("YES");
else:
    print("NO");
 
# This code is contributed by mits


C#




// C# implementation of above approach
using System;
 
class GFG
{
     
// Function to check if it possible
// to form rectangles with heights
// as non-ascending
static int rotateRec(int n, int []L,
                            int []B)
{
 
    // set maximum
    int m = int.MaxValue ;
 
    for (int i = 0; i < n; i++)
    {
        // replace the maximum with
        // previous maximum
        if (Math.Max(L[i], B[i]) <= m)
            m = Math.Max(L[i], B[i]);
 
        // replace the minimum with
        // previous minimum
        else if (Math.Min(L[i], B[i]) <= m)
            m = Math.Min(L[i], B[i]);
 
        // print NO if the above
        // two conditions fail
        // at least once
        else
        {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
public static void Main ()
{
    // initialize the number
    // of rectangles
    int n = 3;
     
    // initialize n rectangles with
    // length and breadth
    int []L = { 5, 5, 6 };
    int []B = { 6, 7, 8 };
    if(rotateRec(n, L, B) == 1)
    Console.WriteLine("YES");
    else
    Console.WriteLine("NO");
}
}
 
// This code is contributed
// by inder_verma


PHP




<?php
// PHP implementation of above approach
 
// Function to check if it possible
// to form rectangles with heights
// as non-ascending
function rotateRec($n, $L, $B)
{
 
    // set maximum
    $m = PHP_INT_MAX;
 
    for ($i = 0; $i < $n; $i++)
    {
        // replace the maximum with
        // previous maximum
        if (max($L[$i], $B[$i]) <= $m)
            $m = max($L[$i], $B[$i]);
 
        // replace the minimum
        // with previous minimum
        else if (min($L[$i], $B[$i]) <= $m)
            $m = min($L[$i], $B[$i]);
 
        // print NO if the above two
        // conditions fail at least once
        else
        {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
 
// initialize the number
// of rectangles
$n = 3;
 
// initialize n rectangles
// with length and breadth
$L = array(5, 5, 6 );
$B = array( 6, 7, 8 );
if(rotateRec($n, $L, $B) == 1)
    echo "YES";
else
    echo "NO";
 
// This code is contributed
// by Shashank
?>


Javascript




<script>
 
// javascript implementation of above approach
 
     
// Function to check if it possible
// to form rectangles with heights
// as non-ascending
function rotateRec( n, L, B)
{
 
    // set maximum
    var m = Number.MAX_VALUE
 
    for (var i = 0; i < n; i++)
    {
        // replace the maximum with
        // previous maximum
        if (Math.max(L[i], B[i]) <= m)
            m = Math.max(L[i], B[i]);
 
        // replace the minimum with
        // previous minimum
        else if (Math.min(L[i], B[i]) <= m)
            m = Math.min(L[i], B[i]);
 
        // print NO if the above
        // two conditions fail
        // at least once
        else
        {
            return 0;
        }
    }
    return 1;
}
 
// Driver code
 
    // initialize the number
    // of rectangles
    var n = 3;
     
    // initialize n rectangles with
    // length and breadth
    var L = [ 5, 5, 6 ];
    var B = [ 6, 7, 8 ];
    if(rotateRec(n, L, B) == 1)
    document.write("YES");
    else
    document.write("NO");
     
</script>


Output: 

NO

 

Time Complexity: O(n), since there runs a loop for n times.
Auxiliary Space: O(1), since no extra space has been taken.



Last Updated : 20 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads