Help me find the mistake i this code

The question statement is BREAKSTICK Problem - CodeChef
The code i wrote is giving a right output against the custom output
the code is

#include <stdio.h>

int main(void) {
	// your code goes here
	int t;
	int n[1000]; 
	int x[1000];
	scanf("%d", &t);
	for(int i = 0; i < t; i++){
	    fscanf(stdin,"%d" "%d", &n[i], &x[i]);
	    getchar();
	}
	for(int i = 0; i < t; i++){
	    if((n[i]%2 == 0) || (x[i] == 1) || (n[i]%x[i] == 0)){
	        printf("YES\n");
	    }
	    else{
	        printf("NO\n");
	    }
	}
	return 0;
}

I have identified a small issue in your code. Please find the corrected version below:

#include <stdio.h>

int main(void) {
int t;
int n[1000];
int x[1000];
scanf(β€œ%d”, &t);
for(int i = 0; i < t; i++){
scanf(β€œ%d %d”, &n[i], &x[i]);
}
for(int i = 0; i < t; i++){
if((n[i] % x[i] == 0) || (n[i] % x[i] == 1)){
printf(β€œYES\n”);
}
else{
printf(β€œNO\n”);
}
}
return 0;
}

  1. In the input section, I combined the format specifier in scanf to read both n[i] and x[i] in a single line: scanf("%d %d", &n[i], &x[i]);.
  2. In the condition for checking if the sticks can be broken into equal parts, I modified the condition to (n[i] % x[i] == 0) || (n[i] % x[i] == 1). This condition checks if the remainder of the division of n[i] by x[i] is either 0 or 1.
1 Like