VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-move-required-to-fit-the-painting-in-frame/

⇱ Minimum Move Required To Fit The Painting in Frame. - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Move Required To Fit The Painting in Frame.

Last Updated : 13 Dec, 2023

Consider a Painting and a Frame having length and breadth as L1, B1 and L2, B2 respectively. Then your task is to output minimum number of moves required to fit that painting into frame by using below operation considering that you are allowed to rotate painting 90° as many times as you want. In each move, you are allowed to cut length or breadth of painting into half . More Formally, update L1 or B1 into (L1/2) or (B1/2).

Note: Fitting painting into frame should be considered as (L1 <= L2 and B1 <= B2) or (L1 <= B2 or L2 <= B1).

Example:

Input: L1 = 8, B1 = 13, L2 = 6, B2 = 10
Output: = 1
Explanation: Fold the Painting by breadth, then (B1/2) = 13/2 = 6. Now the condition (L1 <= B2 or L2 <= B1)holds true

Input: L1 = 4, B1 = 8, L2 = 3, B2 = 10
Output: 1
Explanation: Fold the Painting by length, then (L1/2) = 4/2 = 2. Now, the condition (L1 <= L2 and B1 <= B2).

Approach: Implement the idea below to solve the problem

The main idea to solve the problem is to tracks two counts one for non-rotated placement let say Count1 and another for rotated placement let say Count2. The final result is the minimum of these two representing the minimum number of moves needed for the optimal placement strategy.

Steps were taken to solve the problem:

  • Declare two variables let say Count1 and Count2.
  • Track the number of moves without rotation in Count1 as:
    • In each step, halve the dimensions of the painting (L1 and B1) by 2 if they are larger than the dimensions of the frame (L2 and B2).
  • Track the number of moves with rotation in Count2 as:
    • In each step, halve the dimensions of the painting (L1 and B1) by 2 if they are larger than the dimensions of the frame (L2 and B2).
  • Return min(Count1, Count2).

Below is the code to implement the approach:


Output
1

Time Complexity: O(N)
Auxiliary Space: O(1)

Comment