-
Notifications
You must be signed in to change notification settings - Fork 484
/
two_pointer_on_doubly_linked_list.cpp
117 lines (109 loc) Β· 2.94 KB
/
two_pointer_on_doubly_linked_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//Pair with given sum in sorted doubly linked list
#include<iostream>
#include<vector>
#include<utility>
using namespace std;
typedef long long ll;
//Node structure in doubly linked list
class Node
{
public:
//value present in doubly linked list
ll val;
//next pointer will point to the next node in doubly linked list
Node* next;
//prev pointer will point to the previous node in doubly linked list
Node* prev;
//constructor to initialize node
Node(ll val)
{
this->val=val;
this->next=nullptr;
this->prev=nullptr;
}
};
//Function to insert a node in doubly linked list
void insert(Node** head, ll data)
{
Node* temp=new Node(data);
//Handling the case when the doubly linked list is emoty
if(*head==nullptr)
*head=temp;
else
{
temp->next=*head;
(*head)->prev=temp;
*head=temp;
}
}
//Function for shwoing the linked list
void show(Node* head)
{
Node* h=head;
while(h)
{
cout<<h->val<<"->";
h=h->next;
}
cout<<"NULL"<<endl;
}
//Find pairs with given sum key
vector<pair<ll,ll>> findPair(Node* head, ll key)
{
vector<pair<ll,ll>> ans;
//l pointer will be at the starting of the list
Node* l=head;
Node* h=head;
//h pointer will be at the ending of the list
while(h->next)
h=h->next;
//checking whether l pointer will cross the h pointer
while(l!=h && h->next!=l)
{
//if sum of value of l pointer and h pointer becomes key then the pair will be a possible answer
if(l->val==key-(h->val))
{
ans.push_back({l->val,h->val});
l=l->next;
h=h->prev;
}
//if sum of value of l pointer and h pointer becomes less than key then we need to travel l pointer next to maximize the sum
else if(l->val<key-(h->val))
l=l->next;
//if sum of value of l pointer and h pointer becomes more than key then we need to travel h pointer previous to minize the sum
else
h=h->prev;
}
//After finding all the pairs we retuen the vector of pairs
return ans;
}
int main()
{
Node* head=nullptr;
ll key=7;
// 1->2->3->4->5->6->7->8->9->NULL
insert(&head, 9);
insert(&head, 8);
insert(&head, 7);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 3);
insert(&head, 2);
insert(&head, 1);
//Printing the doubly linked list
show(head);
//Fetching all the pairs with given sum as key
vector<pair<ll,ll>> ans=findPair(head,key);
//Size of ans will be zero if no such pairs found
if(ans.empty())
cout<<"No pair found"<<endl;
else
{
//Printing all possible pairs
cout<<"All possible pairs with given sum as "<<key<<": "<<endl;
for(auto x: ans)
cout<<"("<<x.first<<","<<x.second<<")"<<endl;
}
return 0;
}