For this kind of questions, we need to check the problem carefully...
So there are 45 pupils all in all, and 9 of them gave their sweets away... Therefore, the rest of pupils would be 45-9= 36.
These 36 pupils then got 2 more sweets than they originally have. We can then set x as the number of candies they originally got.
We will then create an equality, since the total number of candies is the same in the 2 scenarios: a) all 45 pupils have candies b) 36 pupils get 2 more
45x = 36(x+2)
45x = 36x + 72
45x - 36x = 72
9x = 72
x = 8 candies
Total number of candies would then be 45x which is 45(8) = 360 candies.