Balancing Problem

 

Problem:  A list of records, L, is given.  Each record in L consists of a pair of integers (a, b).  Modify L to new list L' so that (1) for each integer I, the number of times I appears as the first member of a pair in L' equals the number of times I appears as the second member of a pair in L', (2) every pair (a, b) in L appears in L', either as the pair (a, b), or the pair (b, a), (3) L' is as short as possible, so that properties 1 and 2 hold.