
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find pairs with given sum in doubly linked list in C++
In this problem, we are given a doubly linked list and a value sum. Our task is to find pairs with a given sum in a doubly linked list.
Let’s take an example to understand the problem,
Input
head − 2 <-> 5 <-> 6 <-> 9 <-> 12 x = 11
Output
(2, 9), (5, 6)
Explanation
For pairs (2, 9), the sum of values is 11 For pairs (5, 6), the sum of values is 11
Solution Approach
A simple solution to the problem is traversing the whole linked-list and taking elements one by one and finding the element in the remaining linked list whose sum is sum. This is done by using nested loops.
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; struct Node { int data; struct Node *next, *prev; }; void findSumPairs(struct Node *head, int sum) { struct Node *first = head; int pairCount = 0; while (first != NULL) { struct Node *second = first -> next; while(second != NULL){ if ((first->data + second->data) == sum) { pairCount++; cout<<"("<<first->data<<", "<<second->data<<")\n"; } second = second -> next; } first = first -> next; } if (!pairCount) cout<<"No Such Pairs found !"; } 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; } } int main() { struct Node *head = NULL; insert(&head, 12); insert(&head, 9); insert(&head, 6); insert(&head, 5); insert(&head, 2); int sum = 11; cout<<"Pair in the linked list with sum = "<<sum<<" :\n"; findSumPairs(head, sum); return 0; }
Output
Pair in the linked list with sum = 11 : (2, 9) (5, 6)
Another approach which happens to be more effective is using the fact that the linked list is sorted. For this we will use two pointers, one start initially pointing at the head of the Linked list. And the other end initially pointing at the last node of the Linked list.
Now, we will add then to find the sumVal and then compare it with the
given sum. If sumVal > sum, move end pointer leftwards. If sumVal < sum, move start pointer rightwards. If sumVal == sum, print both values, move start pointer rightwards.
When the pointers cross-each other break out of the loop. Also, we will be counting the number of pairs found, if it is equal to 0, print "No Such Pairs found !"
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; struct Node { int data; struct Node *next, *prev; }; void findSumPairs(struct Node *head, int sum) { struct Node *start = head; struct Node *end = head; while (end->next != NULL) end = end->next; int pairCount = 0; while (start != NULL && end != NULL && start != end && end->next != start) { if ((start->data + end->data) == sum) { pairCount++; cout<<"("<<start->data<<", "<<end->data<<")\n"; start = start->next; end = end->prev; } else if ((start->data + end->data) < sum) start = start->next; else end = end->prev; } if (!pairCount) cout<<"No Such Pairs found !"; } 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; } } int main() { struct Node *head = NULL; insert(&head, 12); insert(&head, 9); insert(&head, 6); insert(&head, 5); insert(&head, 2); int sum = 11; cout<<"Pair in the linked list with sum = "<<sum<<" :\n"; findSumPairs(head, sum); return 0; }
Output
Pair in the linked list with sum = 11 : (2, 9) (5, 6)
- Related Articles
- Find pairs with given product in a sorted Doubly Linked List in Python
- Find pairs with given product in a sorted Doubly Linked List in C++
- Find the largest node in Doubly linked list in C++
- Difference between Singly linked list and Doubly linked list in Java
- Program to find size of Doubly Linked List in C++
- The Doubly Linked List class in Javascript
- Doubly Linked List as Circular in Javascript
- Delete a Doubly Linked List node at a given position in C++
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Reverse a Doubly-Linked List in Groups of a Given Size using C++
- Priority Queue using doubly linked list in C++
- Python Program to Find the Largest Element in a Doubly Linked List
- Count pairs with given sum in C++
