Find pairs with given product in a sorted Doubly Linked List in C++

C++Server Side ProgrammingProgramming

Concept

With respect of a given sorted doubly linked list of positive distinct elements, our taskis to determine pairs in the doubly linked list whose product is equal to given value x,without consuming any extra space.

Input

List = 1 <=> 2 <=> 4 <=> 5 <=> 6 <=> 8 <=> 9
x = 8

Output

(1, 8), (2, 4)

Input

List1 = 1 <=> 2 <=> 3 <=> 4 <=> 5 <=> 6 <=> 7
x = 6

Output

(1, 4), (2,3)

Method

According to a Simple Approach for this problem, we traverse the linked list implementing two nested loops and determine all pairs and verify for the pairs with product equals to x. Here, time complexity for this problem will be O(n^2), where n is total number of nodes in doubly linked list.

An Efficient Solution for this problem is discussed. Here are the steps of the algorithm −

We initialize two pointer variables to determine the candidate elements in the sorted doubly linked list.

We initialize first1 with start of doubly linked list that means, first1=head and initialize second1 with last node of doubly linked list that means, second1=last_node.

Now we initialize first and second pointers as first and last nodes. In this case we don’t have random access, so to determine second pointer, we visit the list to initialize second1.

It has been seen that if current sum of first1 and second1 is smaller than x, then we move first1 in forward direction. Otherwise if current sum of first1 and second1 isgreater than x, then we move second1 in backward direction.

Finally, loop termination conditions are also different from arrays. In this case, the loop ends when either of two pointers become NULL, or they cross each other (second1->next = first1), or they become same (first1 == second1).

Example

 Live Demo

// C++ program to find a pair with
// given product x in sorted Doubly
// Linked List
#include <bits/stdc++.h>
using namespace std;
//Shows Doubly Linked List Node
struct Node1 {
   int data1;
   struct Node1 *next1, *prev1;
};
// Shows function to determine pair whose product
// equal to given value x
void pairProduct(struct Node1* head1, int x1){
   // Now set two pointers,
   // first to the beginning of DLL and second to the end of DLL.
   struct Node1* first1 = head1;
   struct Node1* second1 = head1;
   while (second1->next1 != NULL)
      second1 = second1->next1;
   // Used to track if we find a pair or not
   bool found1 = false;
   // Now the loop terminates when either of two pointers
   // become NULL, or they cross each other (second1->next1
   // == first1), or they become same (first1 == second1)
   while (first1 != NULL && second1 != NULL && first1 != second1 && second1->next1 != first1) {
      // pair found
      if ((first1->data1 * second1->data1) == x1) {
         found1 = true;
         cout << "(" << first1->data1 << ", " << second1->data1 << ")" << endl;
         // Used to move first in forward direction
         first1 = first1->next1;
         // Used to move second in backward direction
         second1 = second1->prev1;
      } else {
         if ((first1->data1 * second1->data1) < x1)
            first1 = first1->next1;
         else
            second1 = second1->prev1;
      }
   }
   // Now if pair is not present
   if (found1 == false)
      cout << "No pair found";
}
// Shows a utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct Node1** head1, int data1){
   struct Node1* temp1 = new Node1;
   temp1->data1 = data1;
   temp1->next1 = temp1->prev1 = NULL;
   if (!(*head1))
      (*head1) = temp1;
   else {
      temp1->next1 = *head1;
      (*head1)->prev1 = temp1;
      (*head1) = temp1;
   }
}
// Driver Code
int main(){
   // Create Doubly Linked List struct Node1* head1 = NULL;
   /*insert(&head1, 9);
   insert(&head1, 8);
   insert(&head1, 6);
   insert(&head1, 5);
   insert(&head1, 4);
   insert(&head1, 2);
   insert(&head1, 1);
   int x1 = 8; */
   insert(&head1, 7);
   insert(&head1, 6);
   insert(&head1, 5);
   insert(&head1, 4);
   insert(&head1, 3);
   insert(&head1, 2);
   insert(&head1, 1);
   int x1 = 6;
   pairProduct(head1, x1);
   return 0;
}

Output

(1, 6)
(2, 3)
raja
Published on 25-Jul-2020 14:40:47
Advertisements