CVDRUN WA problem

i am confused to what is the problem in my code would appreciate if someone could help me.
#include <stdio.h>
int main(){
//initailizing the variables
int T,N,X,Y,K,value;
scanf("%d",&T);//no of test cases
if (T > 100 || T < 1 ){return 0;}
int output[T];
for(int i = 0 ; i < T ; i++){
//getting the input and checking for fault in any constraints

	if (N > 1000 || N < 1 ){return 0;}//no of cities 
	if (K > 1000 || K < 0 ){return 0;}//change factor
	if (X > N-1 || X < 0 ){return 0;}//infected city 
	if (Y > N-1 || Y < 0 ){return 0;}//request status of the city 
	if (N < K){K = K % N ;}//if k is greater than the N
	if(K != 0){

//at zero k will /0 error for this algo so to remove that
for(int i = 0; i < N/K;i++){//it will take only n/k times to return to the same value
value = (X + i * K) % N;
if(value == Y % N){break;}
if (X == Y){value = Y % N;}
else {value = (Y + 1) % N;}
if(value == Y % N){output[i] = 1;}
else{output[i] = 0;}
for(int i = 0 ;i < T;i++){
if (output[i] == 1){printf(“YES\n”);}
else {printf(“NO\n”);}}
return 0;

Three points:

  1. No need to check explicitly for the range of values. i.e. if they say T is in between 1 to 100,
    Then we trust them for it.

  2. Instead of taking all test cases at once and solve them and store answers in array, try to take it one by one. Read one test case calculate solution and print answer.

  3. You are using same variable i in two nested loops, try using different variables, especially in nested loops.

thanks i ll try that

i tried your suggestion and it still gives me WA for the first check second check works fine.

Please give link to your solution. here it is i could change the output coming at the last as that is required in the program but i took the other steps as that you suggested.

Again three points:

  1. Since Y will always between 0 to N-1, so no need to do Y%N every time for comparison.

  2. Since in some case N can be 1. So in this case ( Y% N) and (Y+1)% N give same result.
    So fault in else statement.

  3. I will only give you a hint :wink:. Are you sure only N/K steps are enough for returning to original point.

thanks for the help

1 Like