Open In App

Minimum Move Required To Fit The Painting in Frame.

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

C++




// CPP code to implement the code
 
#include <bits/stdc++.h>
using namespace std;
 
// Method to return minimum number of moves
int minCountToFit(int A, int B, int C, int D)
{
 
    int a = A, b = B, count1 = 0, count2 = 0;
 
    // Fit the painting into the frame with dimensions C
    // x D
    while (a > C || b > D) {
        if (a > C) {
            a /= 2;
            count1++;
        }
        else if (b > D) {
            b /= 2;
            count1++;
        }
    }
 
    a = B;
    b = A;
 
    // Fit the painting into the frame with dimensions C
    // x D while considering rotations
    while (a > C || b > D) {
        if (a > C) {
            a /= 2;
            count2++;
        }
        else if (b > D) {
            b /= 2;
            count2++;
        }
    }
 
    // Return the minimum count of moves considering
    // both rotations and non-rotated placement
    return std::min(count1, count2);
}
 
 
//Driver Function
int main()
{
 
    // Example values for A, B, C, and D
    int L1 = 8, B1 = 13, L2 = 6, B2 = 10;
 
    // Function call
    int minMoves = minCountToFit(L1, B1, L2, B2);
    cout << minMoves;
 
    return 0;
}


Java




// code by flutterfly
public class Main {
    // Method to return the minimum number of moves
    static int minCountToFit(int A, int B, int C, int D) {
        int a = A, b = B, count1 = 0, count2 = 0;
 
        // Fit the painting into the frame with dimensions C x D
        while (a > C || b > D) {
            if (a > C) {
                a /= 2;
                count1++;
            } else if (b > D) {
                b /= 2;
                count1++;
            }
        }
 
        a = B;
        b = A;
 
        // Fit the painting into the frame with dimensions C x D while considering rotations
        while (a > C || b > D) {
            if (a > C) {
                a /= 2;
                count2++;
            } else if (b > D) {
                b /= 2;
                count2++;
            }
        }
 
        // Return the minimum count of moves considering both rotations and non-rotated placement
        return Math.min(count1, count2);
    }
 
    // Driver Function
    public static void main(String[] args) {
        // Example values for A, B, C, and D
        int L1 = 8, B1 = 13, L2 = 6, B2 = 10;
 
        // Function call
        int minMoves = minCountToFit(L1, B1, L2, B2);
        System.out.println(minMoves);
    }
}


Python




# code by flutterfly
def min_count_to_fit(A, B, C, D):
    a, b, count1, count2 = A, B, 0, 0
 
    # Fit the painting into the frame with dimensions C x D
    while a > C or b > D:
        if a > C:
            a //= 2
            count1 += 1
        elif b > D:
            b //= 2
            count1 += 1
 
    a, b = B, A
 
    # Fit the painting into the frame with dimensions C x D while considering rotations
    while a > C or b > D:
        if a > C:
            a //= 2
            count2 += 1
        elif b > D:
            b //= 2
            count2 += 1
 
    # Return the minimum count of moves considering both rotations and non-rotated placement
    return min(count1, count2)
 
# Example values for A, B, C, and D
L1, B1, L2, B2 = 8, 13, 6, 10
 
# Function call
min_moves = min_count_to_fit(L1, B1, L2, B2)
print(min_moves)


C#




// code by Flutterfly
using System;
 
class Program
{
    // Method to return the minimum number of moves
    static int MinCountToFit(int A, int B, int C, int D)
    {
        int a = A, b = B, count1 = 0, count2 = 0;
 
        // Fit the painting into the frame with dimensions C x D
        while (a > C || b > D)
        {
            if (a > C)
            {
                a /= 2;
                count1++;
            }
            else if (b > D)
            {
                b /= 2;
                count1++;
            }
        }
 
        a = B;
        b = A;
 
        // Fit the painting into the frame with dimensions C x D while considering rotations
        while (a > C || b > D)
        {
            if (a > C)
            {
                a /= 2;
                count2++;
            }
            else if (b > D)
            {
                b /= 2;
                count2++;
            }
        }
 
        // Return the minimum count of moves considering both rotations and non-rotated placement
        return Math.Min(count1, count2);
    }
 
    // Driver Function
    static void Main()
    {
        // Example values for A, B, C, and D
        int L1 = 8, B1 = 13, L2 = 6, B2 = 10;
 
        // Function call
        int minMoves = MinCountToFit(L1, B1, L2, B2);
        Console.WriteLine(minMoves);
    }
}


Javascript




// Method to return the minimum number of moves
function minCountToFit(A, B, C, D) {
    let a = A, b = B, count1 = 0, count2 = 0;
 
    // Fit the painting into the frame with dimensions C x D
    while (a > C || b > D) {
        if (a > C) {
            a = Math.floor(a / 2);
            count1++;
        } else if (b > D) {
            b = Math.floor(b / 2);
            count1++;
        }
    }
 
    a = B;
    b = A;
 
    // Fit the painting into the frame with dimensions C x D while considering rotations
    while (a > C || b > D) {
        if (a > C) {
            a = Math.floor(a / 2);
            count2++;
        } else if (b > D) {
            b = Math.floor(b / 2);
            count2++;
        }
    }
 
    // Return the minimum count of moves considering both rotations and non-rotated placement
    return Math.min(count1, count2);
}
 
// Example values for A, B, C, and D
const L1 = 8, B1 = 13, L2 = 6, B2 = 10;
 
// Function call
const minMoves = minCountToFit(L1, B1, L2, B2);
console.log(minMoves);


Output

1




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads