Given Tuple list, compute bidirectional tuples.
Input : test_list = [(5, 6), (1, 2), (6, 5), (9, 1), (6, 5), (2, 1)]
Output : 3
Explanation : (1, 2), (2, 1); (5, 6) -> [(6, 5), (6, 5)], total 3 pairs.
Input : test_list = [(5, 6), (1, 3), (6, 5), (9, 1), (6, 5), (2, 1)]
Output : 2
Explanation : (5, 6) -> [(6, 5), (6, 5)], total 2 pairs.
In this we check for each element if we have any other element which is bidirectional, Once the pair is found, counter is incremented.
OutputThe original list is : [(5, 6), (1, 2), (6, 5), (9, 1), (6, 5), (2, 1)]
Bidirectional pairs count : 3
The time complexity of this solution is O(n), since we only need to loop over the list once and do constant-time lookups in the Counter.
The space complexity is also O(n), since we need to store the frequency of each tuple in the Counter.