I assume that you meant to write:
user X --- membership --> group A
group B -- composition --> group A
If so then you could use the group_element_map view to find what you're looking for:
select count(*)
from group_element_map m1,
group_element_map m2
where m1.group_id = m2.group_id
and m1.element_id = :group_b_id
and m2.element_id = :user_x_id;
The preceeding query will tell you if they belong to a common group either by compostion or by membership. If you need to ensure that they belong to a specific common group, then you could change it to use the following:
select count(*)
from group_element_map m1,
group_element_map m2
where m1.group_id = m2.group_id
and m1.group_id = :group_a_id
and m1.element_id = :group_b_id
and m2.element_id = :user_x_id;