Let's call
t as the number of hours that A need to complete 40 widget.
So A needs t/2 hours to complete 20 widget
and B needs (t-4)/2 hours to complete 20 widget
So to complete 25 widget:
A needs: t/2 * (25/20) = 5t/8 hours
B needs: (t-4)/2 * (25/20) = 5(t-4)/8 hours
Work done by A * B in hours as below:
8/5t + 8/(5t-4) = 1/3
Multiple both sides with 5/8, we have:
1/t - 1/(t-4) = 5/24
Now you can start plugging number. I often started with finding 2 numbers with common mutiple is 24, and the difference between them is 4. Eg: 12 vs 8 are satisfied
1/12 + 1/8 = 5/24.
Bingo!! the answer is 12!
You can read more about this solution via the post named Evading calculation (written by Karishma, reposted by Bunuel)