Find pairs with given sum in doubly linked list in C++

C++Server Side ProgrammingProgramming

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

 Live Demo

#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

 Live Demo

#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)
raja
Published on 16-Mar-2021 04:59:17
Advertisements