Suppose a spring is on a surface. An object of mass $m$ is in $h$ meter height from the top of the spring. When the object falls freely, the spring contracts by $x$ meter. Find $x$ when $k$ is spring constant.
The solution given in books is $mg(h+x)=\frac{1}{2}kx^2$.
Now,let us consider the top of the spring as reference plane. So potential energy at that plane is $0$. Now energy of the system at the beginning is only the potential energy of the object. So $E_1=mgh$
After the fall,the spring contracts by $x$ meter, so the potential energy of the spring is now $\frac{1}{2}kx^2$ and the potential energy of the object is $-mgx$ and kinetic energy of the object is
$\frac{1}{2}mv^2$.
So by conservation of energy,we get
$mgh=-mgx+\frac{1}{2}mv^2+\frac{1}{2}kx^2$.
Where did I go wrong?