Sam is given a rectangular paper having dimensions h*w, where h is the height and w is the width. Sam wants to fold the paper so its dimensions are h1*w1 in the minimum number of moves. The paper can only be folded parallel to its edges and after folding, the dimensions should be integers.
For example, given h=8 and w=4, fold the paper until it is h1, w1 = 6, 1. First fold along the long edge 6 units from a side, resulting in a paper that is 6*4. Next, fold along the width 2 units from the 4 unit edge to have 6*2. Fold along the center of the 2 unit edge to achieve a 6*1 page in three folds.
minMoves has following parameters :
h : an integer that denotes the initial height
w : an integer that denotes the initial width
h1 : an integer that denotes the final height
w1 : an integer that denotes the final width
Constraints
1 <= h, w, h1, w1 <= 10^15
h1 <= h
w1 <= w
Sample Input 0
2
3
2
2
Sample Output
1
i need ANSWER
h=int(input())
w=int(input())
h1=int(input())
w1=int(input())
#x=max(h,w)
flag=True
c=0
while 1:
c=c+1
#print(c)
if flag==True and h!=h1:
h=h-2
#print(“h is”,h)
if h<=h1 and (w==0 or w<=w1):
break
if w!=w1 and flag==False:
w=w-2
#print("w is",w)
if h<=h1 and (w==0 or w<=w1):
break
flag=bool(1-flag)
if c==1:
print(1)
else:
print(c-1)
for input 22
4
6
1
actual output is 4
but it showing output 14
H=Int(Input())
W=Int(Input())
H1=Int(Input())
W1=Int(Input())
c=0
While(h!=h1&&w!=w1)
While(h>h1):
If((h//2)>=(h-h1)):
h=h-(h-h1)
c=c+1
else
h=h//2
c=c+1
While(w>w1):
If((w//2)>=(w-w1)):
w=w-(w-w1)
c=c+1
else
w=w//2
c=c+1
h = int(input())
w = int(input())
h1 = int(input())
w1 = int(input())
c = 0
if h1 > h or w1 > w :
print(“not possible”)
else :
while(h1 != h):
if h1 > h//2:
h = h1
c+=1
else :
h -= h//2
c+=1
while(w1 != w):
if w1 > w//2:
w = w1
c+=1
else :
w -= w//2
c+=1
print(c)
ALL TEST CASES PASSED:-
Python:-
def min_moves(h, w, h1, w1):
folds = 0
while h != h1:
if h1 > h / 2:
folds += 1
h = h1
else:
h /= 2
folds += 1
while w != w1:
if w1 > w / 2:
folds += 1
w = w1
else:
w /= 2
folds += 1
return folds
h = int(input())
w = int(input())
h1 = int(input())
w1 = int(input())
result = min_moves(h, w, h1, w1)
print(result)
I need answer
w=int(input())
h=int(input())
w1=int(input())
h1=int(input())
c=0
while w>w1:
c+=1
if w%2==0:
w=w//2
else:
w=(w+1)//2
while h>h1:
c+=1
if h%2==0:
h=h//2
else:
h=(h+1)//2
print(c)
h=int(input())
w=int(input())
h1=int(input())
w1=int(input())
c=0
while h>h1 :
res=h-h1
if res h//2 :
h=h – h//2
c+=1
while w>w1 :
res=w-w1
if res w//2 :
w=w – w//2
c+=1
print(c)
def fold(h,w,h1,w1):
c=0
if h1>h or w1>w:
return -1
if h1<h:
while h1<(h//2):
c=c+1
h=h//2
c+=1
if w1<w:
while w1<(w//2):
c=c+1
w=w//2
c+=1
print(c)
fold(8,4,8,2)
def fold(h,w,h1,w1):
c=0
if h1>h or w1>w:
return -1
if h1<h:
while h1<(h//2):
c=c+1
h=h//2
c+=1
if w1<w:
while w1<(w//2):
c=c+1
w=w//2
c+=1
print(c)
h,w,h1,w1=list(map(int,input().split()))
fold(h,w,h1,w1)
H,W,H1,W1=map(int,input().split())
c=0
while(H1!=H):
if(H1<(H//2)):
c+=1
H=H//2
else:
c+=1
H=H1
while(W1!=W):
if(W1<(W//2)):
c+=1
W=W//2
else:
c+=1
W=W1
print(c)
Mate your code will give wrong ans for this tc 2 3 2 1
because you have used floor div here
h=int(input())
w=int(input())
h1=int(input())
w1=int(input())
count=0
count1=0
while(h1<h):
h-=1
count+=1
while(w1<w):
w-=1
count1+=1
x=max(count,count1)
print(x)
def getSteps(i,f):
Count=0
If(i!=f):
If(f>=i/2):
Count=1
else:
Count+=getSteps((i-(i//2)),f)
Count+=1
return Count
Ih=int(input())
Iw=int(input())
fh=int(input())
fw=int(input())
Steps=getsteps(ih,fh)
Steps+=getsteps(iw,fw)
Print(steps)
import math
def folds(h,h1):
t=math.ceil(h/2)
if h1h1:
n1=folds(h,h1)
if w>w1:
n2=folds(w,w1)
print(n1+n2)
h=int(input(“enter :”))
w=int(input(“enter :”))
h1=int(input(“enter :”))
w1=int(input(“enter :”))
c=0
while(1):
if(h>h1):
h=h/2
c+=1
if(w>w1):
w=w/2
c+=1
if(h1>=h and w1>=w):
break
import java.util.Scanner;
public class prctc{
public static void main(String[] args)
{
int h,w,h1,w1,c=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the initial height and width of paper”);
h=sc.nextInt();
w=sc.nextInt();
System.out.println(“Enter final height and width of paper”);
h1=sc.nextInt();
w1=sc.nextInt();
while(h>h1) {
if (h1 >= h / 2) {
h = h1;
} else {
h = h – h / 2;
}
c++;
}
while(w>w1)
{
if(w1 >= w/2)
{
w=w1;
}
else
{
w = w – w/2;
}
c++;
}
System.out.println(c);
}
}
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include
using namespace std;
int main()
{
float h,w;
cin>>h>>w;
int h1,w1;
cin>>h1>>w1;
int count=0;
while(h!=h1 || w!=w1){
if(h!=h1){
if(h1>=h/2){
h=h1;
}
else{
h=h/2;
}
count++;
}
if(w!=w1){
if(w1>=w/2){
w=w1;
}
else{
w=w/2;
}
count++;
}
}
cout<<count<<endl;
return 0;
}
import math
H,W=map(int,input().split())
h1,w1=map(int,input().split())
if H==h1:
f1=False
else:
f1=True
if W==w1:
f2=False
else:
f2=True
count=0
while(f1 or f2):
if f1:
half=math.ceil(H/2)
if half=half:
count+=1
f2=False
else:
W=half
count+=1
print(count)
int h,w,h1,w1;
cin>>h>>w>>h1>>w1;
int f=0;
while(h/2 >= h1){
h=h/2;
f++;
}
while(w/2 >= w1){
w=w/2;
f++;
}
if(h1!=h) f++;
if(w1!=w)f++;
cout<<f;
// For example, given h=8 and w=4, fold the paper until it is h1, w1 = 6, 1. First fold along the long edge 6 units from a side, resulting in a paper that is 6*4. Next, fold along the width 2 units from the 4 unit edge to have 6*2.
//Fold along the center of the 2 unit edge to achieve a 6*1 page in three folds.
// PLZ CHECK IF IAM RIGHT
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int h,w,x,y,cnt=0;
cin>>h>>w>>x>>y;
while(x!=h)
{
if((h/2)<x)
{
cnt++;
// cout<=x)
{
h/=2;
cnt++;
}
}
while(y!=w)
{
if((w/2)=y)
{
w/=2;
// cout<<" "<<w<<" " <<y<<endl;
cnt++;
}
}
cout<<cnt<<endl;
return 0;
}
h1=int(input())
w1=int(input())
h2=int(input())
w2=int(input())
h=h1
w=w1
print(h,w)
folds=0
while(h!=h2):
if(h2>h/2):
folds=folds+1
h=h2
print(h,w)
else:
h=h/2
folds=folds+1
print(h,w)
while(w!=w2):
if(w2>w/2):
folds=folds+1
w=w2
print(h,w)
else:
w=w/2
folds=folds+1
print(h,w)
print(folds)
def fold(h,w,h1,w1):
#base
if(h==h1 and w==w1):
return 0;
else:
if(h==h1 and w!=w1):
return 1+fold(h,w-1,h1,w1)
elif(w==w1 and h!=h1):
return 1+fold(h-1,w,h1,w1)
else:
if(hw1):
return 1+fold(h,w-1,h1,w1)
elif(wh1):
return 1+fold(h-1,w,h1,w1)
else:
return 1+fold(h-1,w,h1,w1)
h=int(input())
w=int(input())
h1=int(input())
w1=int(input())
print(fold(h,w,h1,w1))
import math
h=int(input())
w=int(input())
h1=int(input())
w1=int(input())
count=0
while(h>h1):
h=h/2
math.ceil(h)
count+=1
while(w>w1):
w=w/2
math.ceil(w)
count+=1
print(count)
int main(){
int h,w,h1,w1;
cin>>h>>w>>h1>>w1;
int cnt=0;
while(h>=h1 && h!=h1){
if(h/2>=h-h1){
h-=(h-h1);
cnt++;
}
else{
h=h/2;
cnt++;
}
}
while(w>=w1 && w!=w1){
if(w/2>=w-w1){
w-=(w-w1);
cnt++;
}
else{
w=w/2;
cnt++;
}
}
cout<<cnt;
//cout<<cnt;
}
#include
#define ll long long int
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int h, w, h1, w1, cnt1 = 0, cnt2 = 0;
cin >> h >> w >> h1 >> w1;
while (h != h1) {
if (h1 < h / 2) {
h = h / 2;
cnt1++;
} else {
h = h1;
cnt1++;
}
}
while (w != w1) {
if (w1 < w / 2) {
w = w / 2;
cnt2++;
} else {
w = w1;
cnt2++;
}
}
cout << cnt1 + cnt2 << endl;
return 0;
}
import java.util.*;
public class PaperSheets {
static int folds=0;
public static void main(String[] args) {
System.out.println(“Enter input”);
Scanner scan= new Scanner(System.in);
int i_h=scan.nextInt();
scan.nextLine();
int i_w=scan.nextInt();
scan.nextLine();
int f_h=scan.nextInt();
scan.nextLine();
int f_w=scan.nextInt();
scan.close();
int output=solve(i_h,i_w,f_h,f_w);
System.out.println(output);
}
private static int solve(int i_h, int i_w, int f_h, int f_w) {
if(i_h!=f_h)
evaluate(i_h,f_h);
if(i_w!=f_w)
evaluate(i_w,f_w);
return folds;
}
private static void evaluate(int initial, int fin) {
int half=initial/2;
if(initial%2==0) {
// even
if(fin>=half) {
folds++;
}else {
folds++;
evaluate(half,fin);
}
}else {
// odd
if(fin>half) {
folds++;
}else {
folds++;
evaluate(half+1,fin);
}
}
}
}