
- 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 pair for given sum in a sorted singly linked without extra space in C++
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 −
#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)
- Related Articles
- Print reverse of a Linked List without extra space and modification in C Program.
- C++ Program to Implement Sorted Singly Linked List
- C++ Program to Implement Sorted Circularly Singly Linked List
- Rotate a matrix by 90 degree without using any extra space in C++
- Sum of the nodes of a Singly Linked List in C Program
- Find pairs with given product in a sorted Doubly Linked List in C++
- Find a pair with given sum in BST in C++
- Find the Kth Node from the end in a given singly Linked List using C++
- Find middle of singly linked list Recursively in C++
- Search an element in the given singly Linked List using C++
- Find a pair with given sum in a Balanced BST in C++
- Rotate a matrix by 90 degree in clockwise direction without using any extra space in C++
- C++ Program to Delete the First Node in a given Singly Linked List
- Find the Pair with Given Sum in a Matrix using C++
- Find the common nodes in two singly linked list in C++
