http://www.codechef.com/viewsolution/2376002 … is generating WA… i have checked for a few cases and works fine… but can’t figure out why generating wrong answer
http://ideone.com/HvHw4E
You are making some mistakes. In the 1st & 2nd loops iterate till n-1 & m-1 respectively, i.e., i <= n-1 & j <= m-1. I think you are missing some conditions to check too (the if statement), ponder on it for a while and you’ll get it.
for(i=0;i<n-1;i++){
for(j=0;j<m-1;j++){
for(k=j+1;k<m;k++){
if((arr[i][j]==arr[i][k])&&(arr[i][k]==arr[k-j+i][k])&&(arr[k-j+i][k]==arr[k-j+i][j]))
Count++;
}
}
}
All the best.
Regards,
Ouditchya Sinha.
@ouditchya_713: i think there is no need to go till n-1 & m-1 respectively, i.e., i <= n-1 & j <= m-1… as the [i,j] represent the left-top coordinate of the square and there must be some other coordinate which will be the right-top coordinate of the square, since a square can’t have a single coordinate.
i missing some other condition… i don’t know which one.
Suppose input is aa aA, n = 2 , m = 2 ; your loop will then iterate from i = 0 to i < n - 1 => i < 1 , i.e., till i = 0, similarly j = 0 to j = 0, you are not checking 3 cells here (1,0) , (0,1) & (1,1). As for the condition, just think with smaller test cases for your own.
bro, for this… i=0,j=0,k=1… then i check if coordinate [0,0],[0,1][1,0][1,1] have the same value or not… if it is then count++;
looping is fine… i missing some other constraint…
As per my understanding, from each i,j you’re picking k-j length square and checking the elegance.
Consider M > N. Say M=50,N=20. At (0,0) you’d check for 50 length(k from 1 to 49) square as well which doesn’t exist i.e. arr[49][j] should be a seg. fault.
Otherwise seems correct.