Symmetric Pairs

Sort by

recency

|

1542 Discussions

|

  • + 0 comments

    This question has wrong description, something that leads to wrong assumptions. One should not use X1 and X2 in example, it means X1 and X2 are adjecent data points. Instead use Xa and Xp where a and p can be any number from 1 to N, N being the size of the data set.

  • + 0 comments
    select distinct a.x, a.y
    from Functions a
    where a.x <= a.y
      and (select count(*) from Functions where x = a.y and y = a.x)
            >= case when a.x = a.y then 2 else 1 end
    order by a.x;
    
  • + 0 comments

    select f1.x, f1.y from functions f1 join functions f2 on f1.x = f2.y and f1.y = f2.x where f1.x < f1.y

    UNION

    select x,y from functions where x = y group by x,y having count(*) > 1

    order by x

  • + 0 comments

    This problem can be solve using divide and conquer. There are two cases in this problem: 1:(20,20),(20,20)......... there could be many rows like this 2:(22,23),(23,22)....... there could be many rows like this

    if you solve both cases and 'merge' them.

  • + 0 comments

    The part where people get it wrong is that only one record of 1,1 will not generate a symmetric pair. There has two be repeating lines of 1,1 and 1,1. In the dataset there is only 13, 13 and 13, 13 that fullfills this. you have to treat non identical records and repeating identical records in separate queries.

    SELECT DISTINCT non_duplicates.X, non_duplicates.Y FROM ( SELECT X, Y FROM FUNCTIONS WHERE X != Y ) AS non_duplicates JOIN FUNCTIONS AS F2 ON non_duplicates.X = F2.Y WHERE non_duplicates.Y = F2.X AND non_duplicates.X <= non_duplicates.Y

    UNION

    SELECT X, Y FROM FUNCTIONS WHERE X = Y GROUP BY X, Y HAVING COUNT(*) > 1

    ORDER BY X ASC;