One way to think of this is to consider the weight hanging off the table as being a point load at the center of mass. The center of mass is 1/2 of the length that is hanging over the edge, or L/6. The work to lift this force times distance, where
Mass of hanging part=M/3
force = Mg/3, and the distance it needs to be raised is L/6,
hence,
work done = F×distance
Work=Mg/3 ×L/6
W=MgL/18
OR
Shortcut
There is a short cut to calaulate work
It is mgL/(2 ×n^2 )
If 1/n th part is hanging
MgL/ (2×3^2)
MgL/ 2×9
MgL/18