• Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials – Company wise
      • Infosys Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Dell Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • TCS Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation – E Books
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Register
    • Login
  • [email protected]
  • ..
Login / Register
Apply Now
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
0

Cart

Paper Sheets

  • March 5, 2020
  • CODE OF GEEKS
  • 26

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

Tags: hack with infy coding questionshackwithinfy coding problemshackwithinfy coding questions
  • Previous InfyTQ Screening Test Experience 2020 – 7
  • Next Break the Waffle

26 comments on “Paper Sheets”

  1. srikanth says:
    March 8, 2020 at 10:15 am

    i need ANSWER

    Reply
    1. srinidhi says:
      March 8, 2020 at 12:10 pm

      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)

      Reply
      1. ismart_VINODH says:
        March 25, 2020 at 7:03 am

        for input 22
        4
        6
        1
        actual output is 4
        but it showing output 14

        Reply
    2. Rishikesh Jha says:
      March 13, 2020 at 6:46 am

      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

      Reply
    3. Pyla Lohit says:
      April 16, 2021 at 7:57 am

      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)

      Reply
  2. hareesh says:
    March 9, 2020 at 4:30 am

    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)

    Reply
  3. Prathibha says:
    March 10, 2020 at 4:06 am

    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)

    Reply
  4. Anushil says:
    March 10, 2020 at 9:08 am

    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)

    Reply
  5. Anushil says:
    March 10, 2020 at 9:11 am

    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)

    Reply
  6. Lucifer says:
    March 10, 2020 at 1:06 pm

    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)

    Reply
    1. @python_coder7 says:
      March 14, 2020 at 5:47 am

      Mate your code will give wrong ans for this tc 2 3 2 1
      because you have used floor div here

      Reply
  7. Raghav Somani says:
    March 13, 2020 at 7:31 pm

    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)

    Reply
  8. Hiron Saha says:
    March 14, 2020 at 2:31 pm

    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)

    Reply
  9. SURAJ JHA says:
    March 25, 2020 at 6:59 am

    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)

    Reply
  10. himanshu pratap singh says:
    March 29, 2020 at 4:24 pm

    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

    Reply
  11. Himanshu says:
    March 30, 2020 at 4:51 am

    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);
    }
    }

    Reply
  12. Dishant says:
    March 30, 2020 at 7:17 am

    /******************************************************************************

    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;
    }

    Reply
  13. Saurabh singh says:
    April 7, 2020 at 6:30 pm

    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)

    Reply
  14. Sahith says:
    April 18, 2020 at 5:51 pm

    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;

    Reply
  15. Siddesh Vyavahare says:
    April 20, 2020 at 12:39 pm

    // 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;
    }

    Reply
  16. Apoorv Bedmutha says:
    April 21, 2020 at 2:24 pm

    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)

    Reply
  17. Yash Nenwani says:
    May 24, 2020 at 3:42 pm

    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))

    Reply
  18. Tamil Arasan says:
    May 29, 2020 at 2:29 pm

    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)

    Reply
  19. Adesh Kotgirwar says:
    February 18, 2021 at 5:08 am

    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;

    }

    Reply
  20. Easy Boy says:
    April 13, 2021 at 9:42 pm

    #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;
    }

    Reply
  21. anonymous says:
    May 15, 2021 at 9:58 am

    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);
    }
    }
    }

    }

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

What is Amazon Web Services | AWS Regions & AZs | Different AWS Services
  • October 19, 2021
TCS Reasoning 2021
  • October 4, 2021

Copyright 2021 CODE OF GEEKS. All Rights Reserved.

  • →