Ryan and Sam had some sweets in the ratio 2:3. Sam gave 1/3 of his sweets to Ryan. In the end, Ryan had 4 more sweets than Sam. How many sweets did both of them have altogether?

Let the number of sweets Ryan had initially be 2x

Let the number of sweets Sam had initially be 3x
Sam gave 1/3 of his sweets to Ryan, so Sam gave 1/3 * 3x = 1x sweets to Ryan
After Sam gave out 1x sweets, Sam had 3x - 1x = 2x sweets left
After Sam took back 1x sweets, Ryan had 2x + 1x = 3x sweets
Ryan had 4 more sweets than Sam, so 3x = 2x + 4
3x - 2x = 4
1x = 4
Both Ryan and Sam had a total of 2x + 3x = <<2*4+3*4=20>>20 sweets altogether. Answer: \boxed{20}.

Let's assume that Ryan initially had 2x sweets and Sam initially had 3x sweets.

After Sam gave 1/3 of his sweets to Ryan, Ryan had 2x sweets + 1/3 * 3x sweets = 2x + 1x = 3x sweets.
And Sam had 3x sweets - 1/3 * 3x sweets = 3x - 1x = 2x sweets.
Given that Ryan had 4 more sweets than Sam, we can write the equation as follows: 3x sweets = 2x sweets + 4 sweets.
Simplifying the equation, we have: x = 4 sweets.
So, Ryan initially had 2x sweets = 2 * 4 sweets = 8 sweets.
And Sam initially had 3x sweets = 3 * 4 sweets = 12 sweets.
Altogether, they had 8 sweets + 12 sweets = 20 sweets.