At the time the bullet hits the block of the ballistic pendulum, there are no external forces acting on the system. As such linear momentum must be conserved.

Let the gun fire the bullet of mass #m# which travels horizontally with initial velocity #u#. In the absence of external forces, the horizontal component of its velocity will not change. The horizontal component of the bullet's initial linear momentum is

#p_i=m u# .........(1)

After the bullet collides with the block of mass #M# and is trapped inside the pendulum, of mass #(M + m)#, moves with a new horizontal velocity, #v#. The momentum of the system is now:

#p_f=(M+m) v# ............(2)

Since the two momenta are equal, (1) and (2) are equal. Solving for #u#:

#u=(M+m)/m v# .............(3)

Finally, the system acts like a simple pendulum. It moves upward and swings through an angle #theta#. Assuming that mechanical energy is conserved in the whole process, the kinetic energy of pendulum at the bottom of its swing must be equal to its potential energy at the top of swing. Where #h# is the change in height of the center of mass.

#1/2(M+m)v^2=(M+m)gh# ........(4)

We solve for #v#:

#v=sqrt(2gh) # .............(5)

Substitute this value of #v# in (3)

#u=(M+m)/m sqrt(2gh)# ...........(6)

We express #h# in terms of length #L# of the pendulum and angle #theta# of deflection with the help of following figure.

Considering the length #L# at the mean position is made of two parts as below,

#L=Lcos theta +h#

or #h=L-Lcos theta #

or #h=L(1-cos theta) #

Substituting in (6) we have the desired value #u#

#u=(M+m)/m sqrt(2gL(1-cos theta))#