Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in doubly linked list whose sum is equal to given value x, without using any extra space ?
Example:
Input : head : 1 <-> 2 <-> 4 <-> 5 <-> 6 <-> 8 <-> 9
x = 7
Output: (6, 1), (5,2)
Expected time complexity is O(n) and auxiliary space is O(1).
A simple approach for this problem is to one by one pick each node and find second element whose sum is equal to x in the remaining list by traversing in forward direction.Time complexity for this problem will be O(n^2) , n is total number of nodes in doubly linked list.
An efficient solution for this problem is same as this article. Here is the algorithm :
- Initialize two pointer variables to find the candidate elements in the sorted doubly linked list.Initialize first with start of doubly linked list i.e; first=head and initialize second with last node of doubly linked list i.e; second=last_node.
- We initialize first and second pointers as first and last nodes. Here we don’t have random access, so to find second pointer, we traverse the list to initialize second.
- If current sum of first and second is less than x, then we move first in forward direction. If current sum of first and second element is greater than x, then we move second in backward direction.
- Loop termination conditions are also different from arrays. The loop terminates when either of two pointers become NULL, or they cross each other (second->next = first), or they become same (first == second)
C++
// C++ program to find a pair with given sum x. #include<bits/stdc++.h> using namespace std; // structure of node of doubly linked list struct Node { int data; struct Node *next, *prev; }; // Function to find pair whose sum equal to given value x. void pairSum(struct Node *head, int x) { // Set two pointers, first to the beginning of DLL // and second to the end of DLL. struct Node *first = head; struct Node *second = head; while (second->next != NULL) second = second->next; // To track if we find a pair or not bool found = false; // The loop terminates when either of two pointers // become NULL, or they cross each other (second->next // == first), or they become same (first == second) while (first != NULL && second != NULL && first != second && second->next != first) { // pair found if ((first->data + second->data) == x) { found = true; cout << "(" << first->data<< ", " << second->data << ")" << endl; // move first in forward direction first = first->next; // move second in backward direction second = second->prev; } else { if ((first->data + second->data) < x) first = first->next; else second = second->prev; } } // if pair is not present if (found == false) cout << "No pair found"; } // A utility function to insert a new node at the // beginning of doubly linked list void insert(struct Node **head, int data) { struct Node *temp = new Node; temp->data = data; temp->next = temp->prev = NULL; if (!(*head)) (*head) = temp; else { temp->next = *head; (*head)->prev = temp; (*head) = temp; } } // Driver program int main() { struct Node *head = NULL; insert(&head;, 9); insert(&head;, 8); insert(&head;, 6); insert(&head;, 5); insert(&head;, 4); insert(&head;, 2); insert(&head;, 1); int x = 7; pairSum(head, x); return 0; } |
Java
// Java program to find a // pair with given sum x. class GFG { // structure of node of // doubly linked list static class Node { int data; Node next, prev; }; // Function to find pair whose // sum equal to given value x. static void pairSum( Node head, int x) { // Set two pointers, first // to the beginning of DLL // and second to the end of DLL. Node first = head; Node second = head; while (second.next != null) second = second.next; // To track if we find a pair or not boolean found = false; // The loop terminates when either // of two pointers become null, or // they cross each other (second.next // == first), or they become same // (first == second) while (first != null && second != null && first != second && second.next != first) { // pair found if ((first.data + second.data) == x) { found = true; System.out.println( "(" + first.data + ", "+ second.data + ")" ); // move first in forward direction first = first.next; // move second in backward direction second = second.prev; } else { if ((first.data + second.data) < x) first = first.next; else second = second.prev; } } // if pair is not present if (found == false) System.out.println("No pair found"); } // A utility function to insert // a new node at the beginning // of doubly linked list static Node insert(Node head, int data) { Node temp = new Node(); temp.data = data; temp.next = temp.prev = null; if (head == null) (head) = temp; else { temp.next = head; (head).prev = temp; (head) = temp; } return temp; } // Driver Code public static void main(String args[]) { Node head = null; head = insert(head, 9); head = insert(head, 8); head = insert(head, 6); head = insert(head, 5); head = insert(head, 4); head = insert(head, 2); head = insert(head, 1); int x = 7; pairSum(head, x); } } // This code is contributed // by Arnab Kundu |
Output:
(1,6) (2,5)
Time complexity : O(n)
Auxiliary space : O(1)
If linked list is not sorted, then we can sort the list as a first step. But in that case overall time complexity would become O(n Log n). We can use Hashing in such cases if extra space is not a constraint. The hashing based solution is same as method 2 here.
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find pairs with given product in a sorted Doubly Linked List
- Find the largest node in Doubly linked list
- Program to find size of Doubly Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Reverse a Doubly Linked List | Set-2
- QuickSort on Doubly Linked List
- Reverse a Doubly Linked List
- Rotate Doubly linked list by N nodes
- Bubble Sort On Doubly Linked List
- Priority Queue using doubly linked list
- Implementation of Deque using doubly linked list
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Doubly Circular Linked List | Set 2 (Deletion)
- Reverse a doubly circular linked list
Improved By : andrew1234



