Need help in clarifying a doubt related to the Ciel and A-B problem

I am new to codechef. Just started competitive programming. So pardon me if I my question seems naive.

I was solving the “ciel and A-B problem”. I submitted two codes as follows:

#include <stdio.h>
 
int main(){
	int a,b;
	
	scanf("%d %d", &a, &b);
	
	if((a-b)%10==0) printf("%d\n", a-b+1);
	else printf("%d\n", a-b-1);
	
	return 0;
} 

The above code failed.

And the below code passes.

#include <stdio.h>
 
int main(){
	int a,b;
	
	scanf("%d %d", &a, &b);
	
	if((a-b)%10==9) printf("%d\n", a-b-1);
	else printf("%d\n", a-b+1);
	
	return 0;
} 

Please tell me why the first code didn’t work because in the first code I have explicitly checked for the boundary 0 case.

Thanks.

EDIT:

Thanks @kauts_kanu for identifying the failing case. After explicitly checking for the edge case of a-b==1, my below modified code got an AC.

#include <stdio.h>

int main(){
    int a,b;

    scanf("%d %d", &a, &b);

    if((a-b)%10==0) printf("%d\n", a-b+1);
    else {
    	if((a-b)==1) printf("2\n");
    	else printf("%d\n", a-b-1);
    }

    return 0;
}

Strange that 0 is not considered a positive integer.

if A - B = 1 then answer should not be 0. In problem they have mentioned Your answer must be a positive integer

is 0 not a positive integer?

Nope… It’s neither positive nor negative…

Thanks. Didn’t knew it. Learned something new today.

1 Like
//