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.
Thank you for your solution, as I used it as reference to learn. This was harder than at first blush.
Here's my solution with a slight modification on the CTE. I choose not to "PARTITION OVER X,Y", as that makes it more confusing to me.
What I've done is assign a ROW_NUMBER as a UNIQUEIDENTIFIER for the rows. This allows me to join against "all rows that aren't myself" (F1.ID != F2.ID).
Tomayto, tomahto. Cheers! :)
WITH FUNCTIONS_CTE (ID, X, Y) AS (
SELECT ROW_NUMBER() OVER(ORDER BY X), X, Y FROM FUNCTIONS)
SELECT DISTINCT F1.X, F1.Y FROM FUNCTIONS_CTE F1
INNER JOIN FUNCTIONS_CTE F2
ON F1.X = F2.Y
AND F2.X = F1.Y
AND F1.ID != F2.ID
WHERE F1.X <= F1.Y ORDER BY F1.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 →
Thank you for your solution, as I used it as reference to learn. This was harder than at first blush.
Here's my solution with a slight modification on the CTE. I choose not to "PARTITION OVER X,Y", as that makes it more confusing to me.
What I've done is assign a ROW_NUMBER as a UNIQUEIDENTIFIER for the rows. This allows me to join against "all rows that aren't myself" (F1.ID != F2.ID).
Tomayto, tomahto. Cheers! :)