For the Arranging Cupcakes problem ( http://www.codechef.com/problems/RESQ ) I submit a solution using for-loop and it was A.C. I then modified

```
while(tests-->0){
int a = Integer.parseInt(reader.readLine()); //area of cupcake rectangle
int start = (int) Math.sqrt(a); //start checking at dimensions where length and width are nearest
//width >= length because of limit on dimensions analyzed
for(int length = start; start >= 1; length -= 1){
if(a%length == 0){ //optimal side length of the rectangle
//minimum difference between optimal length and width
ans += (a/length - length) + "\r\n";
break;
}
}
}
```

To be (modified code in bold)

```
while(tests-->0){
int a = Integer.parseInt(reader.readLine()); //area of cupcake rectangle
**int length** = (int) Math.sqrt(a); //start checking at dimensions where length and width are nearest
//width >= length because of limit on dimensions analyzed
**while(length-->0){**
if(a%length == 0){ //optimal side length of the rectangle
//minimum difference between optimal length and width
ans += (a/length - length) + "\r\n";
break;
}
**}**
}
```

I then tried the second solution and now it gave me wrong answers and got NZEC on CodeChef. Can anyone please explain to me why this is happening?

I always use while(tests–>0) for my different tests cases. Is there some reason I don’t know about that you shouldn’t access variables being used in situations like while(var–>x) ?

A.C. solution using for(int length = start; start >= 1; length -= 1)

http://www.codechef.com/viewsolution/4340798

Wrong solution using while(length–>0)

http://www.codechef.com/viewsolution/4340808

Thanks!