Last Updated: 28 Sep, 2020

Find Pairs

Easy
Asked in companies
Goldman SachsBank Of AmericaGrant Thornton

Problem statement

We are given a sorted doubly-linked list which contains distinct positive integers, and an integer ‘X’. Print all such unique pairs from the given list so that their sum is equal to ‘X’.

Input format :
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’.
Output format :
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.
Note :
You do not need to print anything, it has already been taken care of. Just implement the function and return the answer. 
Follow Up:
Try to solve this problem in linear time complexity without using any other data structures.
Constraints :
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

Approaches

01 Approach

A simple approach for this problem is to explore all the pairs and print those pairs which have the sum equal to ‘X’.

 

  1. Pick one by one each node and pivot that node.
  2. Find all nodes in the remaining list by traversing in the forward direction such that whose sum with the pivoted node is equal to the ‘X’.

02 Approach

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:

 

  1. While iterating and inserting elements into HashSet, we first look back to check if the current element’s complement already exists in the table. If it exists, We found a pair.
  2. Insert the data of the current node in HashSet.

03 Approach

Since the given doubly linked list is sorted, so here we can use two-pointer techniques. 

The algorithm looks like:

 

  1. Initialise two pointers to find the paired element in the sorted doubly linked list. Let’s say the first pointer is “start” and the second pointer is “end”.
  2. Make “start” as the first node of the doubly linked list and “end” as the last node of the doubly linked list. Here we don’t have random access property, so we need to find the last node of the doubly linked list by traversing the entire list.
  3. If the sum of data at the “start” node and “end” node is less than ‘X’, then we want to increase the sum so we will move the “start” pointer forward to get a larger value node.
  4. If the sum of data at the “start” node and “end” node is greater than ‘X’, then we want to decrease the sum so we will move the “end” pointer backwards to get a smaller value node.
  5. Loop will terminate when either of “start” or “end” become NULL, or when they cross each other, i.e. (end->next==start).