Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Drawing Book problem solution

YASH PAL, 31 July 20249 August 2024

In this Drawing Book problem you have Given n and p, find and print the minimum number of pages that must be turned in in order to arrive at page p.

Hackerrank drawing book solution
Hackerrank drawing book solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

#!/bin/python3

import sys


n = int(input().strip())
p = int(input().strip())
# your code goes here

page_in_book = p//2
total_pages = n//2

from_front = page_in_book
from_back = total_pages - page_in_book
print(min(from_front,from_back))

 


Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int p = in.nextInt();
        int frontdist = p / 2;
        int backdist = n % 2 == 0 ? (n - p + 1) / 2 : (n - p) / 2;
        System.out.println(Math.min(frontdist, backdist));
        // your code goes here
    }
}

 

Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int n;
    cin >> n;
    int p;
    cin >> p;
    int min_left = 0;
    int cur_left = 0, cur_right = 1;
    while (cur_left != p && cur_right != p) {
        ++min_left;
        cur_left += 2;
        cur_right += 2;
    }
    int min_right = 0;
    cur_left = n % 2 == 0 ? n : n - 1;
    cur_right = cur_left + 1;
    
    while (cur_left != p && cur_right != p) {
        ++min_right;
        cur_left -= 2;
        cur_right -= 2;
    }
    
    cout << std::min(min_left, min_right);
    return 0;
}

 

Problem solution in C programming.

#include <stdio.h>
int main()
    {
    int n,p,j=0,a[100000];
    scanf("%d",&n);
    scanf("%d",&p);
    for (int i=0;i<=n;i++)
        if (i%2==0)
        {
        j++;
        a[i]=j;
        a[i+1]=j;
    }
    if (a[p]-1<j-a[p]) printf("%d",a[p]-1);
    else printf("%d",j-a[p]);
    return 0;
}

 

Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
  var n = parseInt(readLine());
  var p = parseInt(readLine());
  var count = 0;
  var page = 1;
  while(page < p){
    count++;
    page += 2;
  }
  if(n % 2 !== 0){
    page = n - 1;
  } else {
    page = n;
  }
  var countTwo = 0;
  while(page > p){
    countTwo++;
    page -= 2;
  }
  if(count < countTwo){
    console.log(count);
  } else {
    console.log(countTwo);
  }
}

 

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes