-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pairs_with_given_sum_in_doubly_linked_list.cpp
85 lines (69 loc) · 1.7 KB
/
Pairs_with_given_sum_in_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
/*
Problem Statement:
-----------------
Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in a doubly-linked list
whose sum is equal to given value x, without using any extra space?
Example:
--------
Input : head : 1 <-> 2 <-> 4 <-> 5 <-> 6 <-> 8 <-> 9
sum = 7
Output: (6, 1), (5,2)
The expected time complexity is O(n) and auxiliary space is O(1).
*/
// Link --> https://www.geeksforgeeks.org/find-pairs-given-sum-doubly-linked-list/
// Code:
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next , *prev;
};
void insertNode(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;
}
}
void findPairSum(struct Node *head , int sum)
{
struct Node *start = head;
struct Node *last = head;
while(last->next)
last = last->next;
while(start->next && last->prev)
{
if(start->data + last->data == sum)
{
cout<<"Pair sum is present : ("<<start->data<<" , "<<last->data<<")\n";
start = start->next;
last = last->prev;
}
else if(start->data + last->data > sum)
last = last->prev;
else
start = start->next;
}
}
int main()
{
struct Node *head = NULL;
insertNode(&head, 9);
insertNode(&head, 8);
insertNode(&head, 6);
insertNode(&head, 5);
insertNode(&head, 4);
insertNode(&head, 2);
insertNode(&head, 1);
int sum = 15;
findPairSum(head , sum);
return 0;
}