Let’s consider the initial position
of the ball to be at the origin of a Cartesian coordinate system. Vertically the acceleration is constant and horizontally there is no acceleration so in both cases
we can use SUVAT equations of motion. Considering its horizontal motion we have x=ut cos(theta) and by considering its vertical motion we have y=ut sin(theta) – 1/2 g t^2. By eliminating t from these equations we get the equation for the trajectory of the ball. if we set y equal to 0 then the solutions to the resulting equation are the initial position of the ball x=0 and another solution x=(2 v^2)/(g) sin(theta) cos(theta) which is the solution we’re interested in.
Now we must find the angle theta which maximizes this expression. You will find this easier if you first make use of the identity sin(2 theta)=2 sin(theta) cos(theta) to simplify the expression.