If the second half of the loop is frictionless, this is just a simple application of Newton's second law. In order to stay on the loop, the cube needs to move in a circle of radius $r$, but no smaller, which means it has to be subject to a net inward force of $mv^2/r$, but no larger. Gravity always provides a force of $mg$, so if $g > v^2/r$, the inward force will be too large for the block to stay on the loop.
If there is friction on the second half of the loop, you could use the same idea in principle, but it becomes a lot messier because you need to check the force at every point on the second half, not just the top. Consider a point at angle $\theta$ around the loop, where $\theta = 0$ corresponds to the very top. If you draw a free-body diagram, you'll see that there are two forces acting perpendicular to the loop (gravity and the normal force) and two components acting parallel to the loop (gravity and friction). Write out Newton's second law in each case and you get
$$mg\cos\theta + F_N(\theta) = m r\dot\theta^2$$
(perpendicular) and
$$mg\sin\theta - \mu F_N(\theta) = mr\ddot\theta$$
(parallel). Notice that I've written the centripetal acceleration ($r\dot\theta^2$) and tangential acceleration ($r\ddot\theta$) in polar coordinates for convenience.
This is a system of coupled differential equations that you would need to solve to find the normal force at each angle. If $F_N(\theta) < 0$ for any $\theta$, the block will fall off at that point. Of course, those are rather complicated equations, and I'm pretty sure you could only solve them numerically.
You could also try using the Euler-Lagrange equation and Lagrange multipliers for the case with friction. I don't have time to write that up now but I'll come back to this as soon as I can and fill it in.