Drawing Book

 Question :-

https://www.hackerrank.com/challenges/drawing-book/problem

Solution in C language :-

#include<stdio.h>
int main()
{
    int i,n,p;
    scanf("%d %d",&n,&p);

    if((n/2)>=p){
        //printf("*1*");
        if(p==1){
            printf("0");
        }
        for(i=1;i<=(n/2);i+=2){
            if(i+1==p || i+2==p)
            printf("%d",(i/2)+1);
        }
    }
    if((n/2)<p && (n%2)==0){
        //printf("*2*");
        if(p==n){
            printf("0");
        }
        else{
        for(i=1;i<(n/2);i+=2){
            if(n-i==p || n-i-1==p)
            printf("%d",(i/2)+1);
        }
        }
    }
    if((n/2)<p && (n%2)==1){
        //printf("*3*");
        if(p==n || p==n-1){
            printf("0");
        }
        else{
        for(i=1;i<(n/2);i+=2){
            if((n-i-1)==p || (n-i-2)==p)
            printf("%d",(i/2)+1);
        }
        }
    }
    return 0;
}

Comments

Popular posts from this blog

Sherlock and Squares

Cut The Sticks

Home