Recent Post
Drawing Book.c-HackerRank
- Get link
- X
- Other Apps
Problem- Brie’s Drawing teacher asks her class to open their books to a page
number. Brie can either start turning pages from the front of the book or from
the back of the book. She always turns pages one at a time. When she opens the
book, page is always on the right side: When she
flips page 1,she sees 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 she wants to turn to page p, what is the
minimum number of pages she will turn? She can start at the beginning or the
end of the book. Given n and p,
find and print the minimum number of pages Brie must turn in order to arrive at
page p.
Solution-
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n,p,diff,page;
scanf("%d\n%d",&n,&p);
diff=n-p;
if(diff<p)
{
if(n==p)
printf("0");
else
{
if(n%2==0)
{
page=1+(diff-1)/2;
printf("%d",page);
}
else
printf("%d",diff/2);
}
}
else
printf("%d",p/2);
}
Link to the problem:-
https://www.hackerrank.com/challenges/drawing-book/problem
- Get link
- X
- Other Apps
Comments
Popular posts from this blog
Caesar Cipher.c-HackerRank
Problem- Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's Cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c. Original alphabet: abcdefghijklmnopqrstuvwxyz Alphabet rotated +3: defghijklmnopqrstuvwxyzabc For example, the given cleartext and the alphabet is rotated by . The encrypted string is . Note: The cipher only encrypts letters; symbols, such as - , remain unencrypted. Solution- #include < assert.h > #include < limits.h > #include < math.h > #include < stdbool.h > #include < stddef.h > #include < stdint.h > #include < stdio.h > #include < stdlib.h > #include < string.h > int mai...
Bon Appétit.c-HackerRank
Problem- Anna and Brian are sharing a meal at a restaurant and they agree to split the bill equally. Brian wants to order something that Anna is allergic to though, and they agree that Anna won't pay for that item. Brian gets the check and calculates Anna's portion. You must determine if his calculation is correct. Solution- #include < assert.h > #include < ctype.h > #include < limits.h > #include < math.h > #include < stdbool.h > #include < stddef.h > #include < stdint.h > #include < stdio.h > #include < stdlib.h > #include < string.h > int main() { int n,k,sum= 0 ,i,sum2= 0 ; int price= 0 ,extra= 0 ; scanf( "%d %d" ,&n,&k); int arr[n]; for (i= 0 ;i<n;i++) { scanf( "\n%d " ,&arr[i]); } ...
Electronics Shop.c-HackerRank
Problem- Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the 2 items, given her budget. Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items . Solution- Code for the function provided:- int getMoneySpent( int n, int * keyboards, int m, int * drives, int b) { int sum,max=- 1 ; for ( int i= 0 ;i<n;i++) { for ( int j= 0 ;j<m;j++) { ...
👍
ReplyDelete