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:
C++
#include <bits/stdc++.h>
using namespace std;
int minCountToFit( int A, int B, int C, int D)
{
int a = A, b = B, count1 = 0, count2 = 0;
while (a > C || b > D) {
if (a > C) {
a /= 2;
count1++;
}
else if (b > D) {
b /= 2;
count1++;
}
}
a = B;
b = A;
while (a > C || b > D) {
if (a > C) {
a /= 2;
count2++;
}
else if (b > D) {
b /= 2;
count2++;
}
}
return std::min(count1, count2);
}
int main()
{
int L1 = 8, B1 = 13, L2 = 6, B2 = 10;
int minMoves = minCountToFit(L1, B1, L2, B2);
cout << minMoves;
return 0;
}
|
Java
public class Main {
static int minCountToFit( int A, int B, int C, int D) {
int a = A, b = B, count1 = 0 , count2 = 0 ;
while (a > C || b > D) {
if (a > C) {
a /= 2 ;
count1++;
} else if (b > D) {
b /= 2 ;
count1++;
}
}
a = B;
b = A;
while (a > C || b > D) {
if (a > C) {
a /= 2 ;
count2++;
} else if (b > D) {
b /= 2 ;
count2++;
}
}
return Math.min(count1, count2);
}
public static void main(String[] args) {
int L1 = 8 , B1 = 13 , L2 = 6 , B2 = 10 ;
int minMoves = minCountToFit(L1, B1, L2, B2);
System.out.println(minMoves);
}
}
|
Python
def min_count_to_fit(A, B, C, D):
a, b, count1, count2 = A, B, 0 , 0
while a > C or b > D:
if a > C:
a / / = 2
count1 + = 1
elif b > D:
b / / = 2
count1 + = 1
a, b = B, A
while a > C or b > D:
if a > C:
a / / = 2
count2 + = 1
elif b > D:
b / / = 2
count2 + = 1
return min (count1, count2)
L1, B1, L2, B2 = 8 , 13 , 6 , 10
min_moves = min_count_to_fit(L1, B1, L2, B2)
print (min_moves)
|
C#
using System;
class Program
{
static int MinCountToFit( int A, int B, int C, int D)
{
int a = A, b = B, count1 = 0, count2 = 0;
while (a > C || b > D)
{
if (a > C)
{
a /= 2;
count1++;
}
else if (b > D)
{
b /= 2;
count1++;
}
}
a = B;
b = A;
while (a > C || b > D)
{
if (a > C)
{
a /= 2;
count2++;
}
else if (b > D)
{
b /= 2;
count2++;
}
}
return Math.Min(count1, count2);
}
static void Main()
{
int L1 = 8, B1 = 13, L2 = 6, B2 = 10;
int minMoves = MinCountToFit(L1, B1, L2, B2);
Console.WriteLine(minMoves);
}
}
|
Javascript
function minCountToFit(A, B, C, D) {
let a = A, b = B, count1 = 0, count2 = 0;
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;
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 Math.min(count1, count2);
}
const L1 = 8, B1 = 13, L2 = 6, B2 = 10;
const minMoves = minCountToFit(L1, B1, L2, B2);
console.log(minMoves);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...