We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
WITH SMALLER AS
(
SELECT DISTINCT X,Y
FROM FUNCTIONS
WHERE XY
),
SAME AS
(
SELECT
X,Y,
COUNT(*) AS COUNTSAME
FROM FUNCTIONS
WHERE X=Y
GROUP BY X, Y
)
SELECT SMALLER.X,SMALLER.Y
FROM SMALLER
JOIN BIGGER ON (SMALLER.X=BIGGER.Y AND SMALLER.Y=BIGGER.X)
UNION ALL
SELECT DISTINCT X,Y
FROM
SAME
WHERE MOD(COUNTSAME,2)=0
ORDER BY X
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Symmetric Pairs
You are viewing a single comment's thread. Return to all comments →
WITH SMALLER AS ( SELECT DISTINCT X,Y FROM FUNCTIONS WHERE XY ), SAME AS ( SELECT X,Y, COUNT(*) AS COUNTSAME FROM FUNCTIONS WHERE X=Y GROUP BY X, Y ) SELECT SMALLER.X,SMALLER.Y FROM SMALLER JOIN BIGGER ON (SMALLER.X=BIGGER.Y AND SMALLER.Y=BIGGER.X) UNION ALL SELECT DISTINCT X,Y FROM SAME WHERE MOD(COUNTSAME,2)=0 ORDER BY X