Breaking

Monday 14 August 2017

Turning Book Pages

                            Turning Book Pages





A book can be turned either from front or from the back. When we open a book always we have the page 1 always on the right. The book pages must always be turned one by one instead of multiple pages at once. Each page has two sides, front and back. Hence the last page may have only front side depending on the total number of pages N in the book (If N is even, it will have both sides printed else if N is odd only the front side will be printed).
Now Manoj wants to navigate to a page P in the book by turning the least minimum pages either from front or back. Please help Manoj by completing the program as per the given requirement.
Input Format:
The first line will contain the value of N which represents the total number of pages in the book.
The second line will contain the value of P which represents the page to be navigated.
Output Format:w
The first line will contain the integer value which is the least minimum pages to be turned either from front or back.
Constraints:
1 <= N <= 10000
1 <= P <= N
Example Input/Output 1:
Input:
8
6
Output:
1
Explanation:
From front, after turn 1, pages 2 & 3 are visible. After turn 2, pages 4 & 5 are visible. In the third turn pages 6 & 7 are visible. So 3 turns are required from front.
From back the last page back side is page 8. So after turn 1, pages 6 & 7 are visible. So 1 turn is required from the back.
The minimum of 3 and 1 (which is 1) is printed as the output.

Example Input/Output 2:
Input:
12
4
Output:
2
Explanation:
From front, after turn 1, pages 2 & 3 are visible. After turn 2, pages 4 & 5 are visible. So 2 turns are required from front.
From back the last page back side is page 12. So after turn 1, pages 10 & 11 are visible. After turn 2, pages 8 & 9 are visible. After turn 3, pages 6 & 7 are visible. Only after turn 4, pages 4 and 5 are visible. So 4 turns are required from the back.
The minimum of 2 and 4 (which is 2) is printed as the output.

Code:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int n,p,i,front,back,f=0,b=0;
cin>>n>>p;
front=1;back=n;
if(n%2==1)
    --back;
for(i=0;i<n;i++)
{
    if(front>=p)
       break;
    else
     {   front+=2;f++;}
    if(back<=p)
        break;
    else
        {back-=2;b++;}
}
if(f<=b)
    cout<<f;
else
    cout<<b;

}

Please do comment If u have any Queries!

2 comments:

  1. import java.util.*;
    public class Hello {

    public static void main(String[] args) {
    //RMK FACULTY

    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int p = sc.nextInt();

    int frontcount=0;
    for(int i=0;ip;)
    {
    if(i==n)
    {
    i--;
    backcount++;
    }
    else
    {
    i-=2;
    backcount++;
    }
    }

    if(n%2==1)
    {
    backcount--;
    frontcount--;
    }

    if(p==1)
    frontcount--;

    if(frontcount<backcount)
    System.out.println(frontcount);
    else
    System.out.println(backcount);

    }
    }

    ReplyDelete
  2. //Without loop

    import java.util.*;
    public class TurningThePages {

    public static void main(String[] args) {
    Scanner s=new Scanner( System.in);
    int n=s.nextInt(),p=s.nextInt();
    int front=0,back=0;
    if(n%2==1)
    n--;
    if(p%2==1)
    p--;
    if(p!=1)
    front=(int)Math.floor(p/2);
    if((n-p)!=1)
    back=(int)Math.floor((n-p)/2);
    System.out.println(Math.min(front,back));
    }
    }

    ReplyDelete

Like