Mar 1, 2009 at 10:35pm UTC
is there some difference between the given two ways
for(int i=0;i<=height;i++){
for(int j=0;j<=width;j++){
if(i!=j) sum+=(width-j)*(height-i);
}
}
here i am just taking into account those values in which i and j have different values and 2nd method is
for(int i=0;i<=height;i++){
for(int j=0;j<=width;j++){
sum+=(width-j)*(height-i);
}
sum-=(width-i)*(height-i);
}
here i am adding those vales as well in my solution but later subtracting them
the 2nd approach is giving the wrong answer but i thing that both are logically same.
???
Mar 1, 2009 at 11:04pm UTC
May as well substitute the "<="s for "<" as whenever they are equal you are adding 0. Aside from that, give example numbers. I'm getting the same for both.
Mar 2, 2009 at 12:58am UTC
well. using "<=" or "<" is not a big prob as it just forcing just 1 more execution of the loop.
here is the sample input
width=592
height=964
the 1st method is giving the right answer that is 81508708664
but going through by 2nd method i am is getting -126050230
Mar 2, 2009 at 1:52am UTC
Big numbers. Are you initializing sum to 0 before attempting the second method?
And yeah, it's no big problem but may as well be as efficient as possible.