Work-Energy Theorem states Sum of work done by all forces on a body is equal to the change of kinetic energy of the body.
∑iWj=12m(v2f−v2i)
where Wj is the work done by the j force, and vf and vi are final and initial velocities
We know spring force F=−kx, where k is called the spring constant.
Hooke law states the spring force F=−kx
We consider a string of string constant k and initial elongation x we stretch it by an amount dx
So, Work done to stretching The spring gets stared as elastic potential energy in it and is equal to
dU=dW=F dxcos180o [∵ The force and the resulting displacement are anti-parallel]
Integrating with proper limits
Uelastic=∫x0−kx dx(−1)=12k[x2]x0
Or Uelastic=12kx2