Let A and B have x and y apples respectively.
If A gives one apple to B then,
x-1 = y+1
=> x = y +2 --------------------(1)
if B gives one apple to A then,
x+1 = 3(y-1) ---------------(2)
On putting the value of x from eq(1) in eq(2) we get
(y + 2) + 1 = 3y -3
y +3 = 3y -3
3y-y = 3+3
2y = 6
y=3
So, B had 3 apples and
A had y+2 = 3+2 = 5 apples