(a) at spring was initially compressed by a distance x and was finally in its natural length
(b) it was initially stretched by a distance x and and finally was in its natural length
For an elastic spring, the work done is equal to the negative of the change in its potential energy.
When the spring was initially compressed or stretched by a distance x, its potential energy is given by
.
When it finally comes to its natural length, its potential energy is given by
.
∴ Work done =