Since they are similar, you need to find the ratio of similarity (I made up the term, there is probably a correct one that I can’t remember).
If you divide 16/40, you’ll find that that ratio is 2.5. So then you just multiply 16 x 2.5. You’ll get 18.
18 is the length of the top of the trapezoid.
You set 18=2x+4 and solve it algebraically. Subtract 4 from both sides.
14=2x
Divide by 2 and x=7
(You can also check that the ratio is right by 16/18 is the same decimal value as 40/45. You’ll get .88888...)