When they flip page 1, they see pages 2 and 3. Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is n pages long, and a student wants to turn to page p, what is the minimum number of pages to turn? They can start at the beginning or the end of the book.
Given n and p, find and print the minimum number of pages that must be turned in order to arrive at page p.
Example
n=5
p=3
Using the diagram above, if the student wants to get to page 3, they open the book to page 1, flip 1 page and they are on the correct page. If they open the book to the last page, page 5, they turn 1 page and are at the correct page. Return1 .
Function Description
Complete the pageCount function in the editor below.
pageCount has the following parameter(s):
- int n: the number of pages in the book
- int p: the page number to turn to
Returns
- int: the minimum number of pages to turn
Input Format
The first line contains an integer n, the number of pages in the book.
The second line contains an integer,p, the page to turn to.
Constraints
- 1<=n<=10^5
- 1<=p<=n
Sample Input 0
6 2
Sample Output 0
1
Drawing book HackerRank Solution in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n; scanf("%d",&n); int p; scanf("%d",&p); int ans1,ans2; ans1=p/2; ans2=(n-p)/2; if(ans1>ans2) { printf("%d",ans2); } else { printf("%d",ans1); } return 0; }
Drawing book HackerRank Solution in C++
#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; cout<<min(p/2,(n-p)/2); // your code goes here return 0; }
Drawing book HackerRank Solution in Java
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(); // your code goes here int ans = Math.min(p/2,(n-p)/2); System.out.println(ans); } }
Drawing book HackerRank Solution in Python
#!/bin/python import sys n = int(raw_input().strip()) p = int(raw_input().strip()) # your code goes here print min(p/2,(n-p)/2) Drawing book HackerRank Solution in C# using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); int p = Convert.ToInt32(Console.ReadLine()); int front = p / 2; int back = (n / 2) - front; Console.WriteLine(Math.Min(front,back)); } }
Attempt – Drawing Book HackerRank Challenge
Link – https://www.hackerrank.com/challenges/drawing-book/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/counting-valleys-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.
주변에도 추천해서 더 많은 사람들이 알았으면 좋겠어요~ 방문하다 먹튀레이더