


The first line of the input contains the elements of the doubly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
The second line contains a single integer ‘X’.
Print pair elements separated by a single pace where the first element of the pair should be less than the second element of the pair. The order of pairs does not matter.
Print each unique pair in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the function and return the answer.
Try to solve this problem in linear time complexity without using any other data structures.
1 <= N <= 5*10^5
-2*10^9 <= X <= 2*10^9
-10^9 <= data <= 10^9 and data != -1
Where ‘N’ is the length of the linked list and ‘X’ is the required pair sum value.
Time Limit: 1 sec
A simple approach for this problem is to explore all the pairs and print those pairs which have the sum equal to ‘X’.
To improve our run time complexity, we need a more efficient way to check if the complement exists in the doubly linked list or not. If the complement exists, then print the complement and the data of the current node.
Steps:
Since the given doubly linked list is sorted, so here we can use two-pointer techniques.
The algorithm looks like: