Find pair for given sum in a sorted singly linked without extra space in C++

C++Server Side ProgrammingProgramming

Suppose we have a singly linked list and a value x; we have to find a pair whose sum is same as x. We have to keep in mind that we cannot use any extra space and expected time complexity will be O(n).

So, if the input is like 4→7→8→9→10→11→12, x = 19, then the output will be [(7, 12), (8, 11), (9, 10)]

To solve this, we will follow these steps −

  • Define a function convert_to_xor(), this will take start,

  • prev := NULL

  • while start is NULL, do −

    • next_list_node := next of start

    • next of start := XOR of the address of next_list_node and prev

    • prev := start

    • start := next_list_node

  • From the main method, do the following −

  • first := start

  • next_list_node := NULL, prev := NULL, second := start

  • while next of second is not equal to prev, do −

    • temp := second

    • second := XOR of the address of (next of second, prev)

    • prev := temp

  • next_list_node := NULL

  • prev := NULL

  • flag := false

  • while (first is not equal to NULL and second is not equal to NULL and first is not equal to second and first is not equal to next_list_node), do −

    • if data of first + data of second is same as x, then −

      • display pair data of first, data of second

      • flag := true

      • temp := first

      • first := XOR of the address of (next of first,prev)

      • prev := temp

      • temp := second

      • second := XOR of the address of next of second, next_list_node)

      • next_list_node := temp

    • Otherwise

      • if data of first + data of second < x, then

        • temp := first

        • first := XOR of the address of (next of first,prev)

        • prev := temp

      • Otherwise

        • temp := second

        • second := XOR of the address of (next of second, next_list_node)

        • next_list_node := temp

  • if flag is same as false, then −

    • there is no pair

Example (C++)

Let us see the following implementation to get better understanding −

 Live Demo

#include<bits/stdc++.h>
using namespace std;
class ListNode {
public:
   int data;
   ListNode *next;
   ListNode(int data) {
      this->data = data;
      next = NULL;
   }
};
ListNode *make_list(vector<int> v) {
   ListNode *start = new ListNode(v[0]);
   for (int i = 1; i < v.size(); i++) {
      ListNode *ptr = start;
      while (ptr->next != NULL) {
         ptr = ptr->next;
      }
      ptr->next = new ListNode(v[i]);
   }
   return start;
}
ListNode* XOR (ListNode *a, ListNode *b) {
   return (ListNode*) ((uintptr_t) (a) ^ (uintptr_t) (b));
}
void convert_to_xor(ListNode *start) {
   ListNode *next_list_node;
   ListNode *prev = NULL;
   while (start != NULL) {
      next_list_node = start->next;
      start->next = XOR(next_list_node, prev);
      prev = start;
      start = next_list_node;
   }
}
void get_pared_sum(ListNode *start, int x) {
   ListNode *first = start;
   ListNode *next_list_node = NULL, *prev = NULL;
   ListNode *second = start;
   while (second->next != prev) {
      ListNode *temp = second;
      second = XOR(second->next, prev);
      prev = temp;
   }
   next_list_node = NULL;
   prev = NULL;
   bool flag = false;
   while (first != NULL && second != NULL && first != second && first != next_list_node) {
      if ((first->data + second->data)==x) {
         cout << "(" << first->data << ","<< second->data << ")" << endl;
         flag = true;
         ListNode *temp = first;
         first = XOR(first->next,prev);
         prev = temp;
         temp = second;
         second = XOR(second->next, next_list_node);
         next_list_node = temp;
      }
      else{
         if ((first->data + second->data) < x) {
            ListNode *temp = first;
            first = XOR(first->next,prev);
            prev = temp;
         }
         else{
            ListNode *temp = second;
            second = XOR(second->next, next_list_node);
            next_list_node = temp;
         }
      }
   }
   if (flag == false)
      cout << "No pair found" << endl;
}
int main() {
   vector<int> v = {4,7,8,9,10,11,12};
   ListNode* start = make_list(v);
   int x = 19;
   convert_to_xor(start);
   get_pared_sum(start,x);
}

Input

{4,7,8,9,10,11,12}

Output

(7,12) (8,11) (9,10)
raja
Published on 25-Jul-2020 10:59:14
Advertisements