Find quadruplets with given sum in a Doubly Linked List
Last Updated :
30 Nov, 2021
Given a sorted doubly linked list and an integer X, the task is to print all the quadruplets in the doubly linked list whose sum is X.
Examples:
Input: LL: -3 ? 1 ? 2 ? 3 ? 5 ? 6, X = 7
Output:
-3 2 3 5
-3 3 1 6
Explanation: The quadruplets having sum 7( = X) are: {-3, 2, 3, 5}, {-3, 3, 1, 6}.
Input: LL: -2 ? -1 ? 0 ? 0 ? 1 ? 2, X = 0
Output:
-2 -1 1 2
-2 0 0 2
-1 0 0 1
Approach: The given problem can be solved by using the idea discussed in this article using the 4 pointer technique. Follow the steps below to solve the problem:
- Initialize four variables, say first as the start of the doubly linked list i.e.; first = head, second to the next of the first pointer, third to the next of second pointer, and fourth to the last node of a doubly linked list that stores all the 4 elements in the sorted doubly linked list.
- Iterate a loop until the first node and the fourth node is not NULL, and they are not equal and these 2 nodes do not cross each other and perform the following steps:
- Initialize the second pointer to the next of the first.
- Iterate a loop until the second and fourth nodes are not NULL, they are not equal and do not cross each other.
- Initialize a variable, say sum as (X – (first?data + second?data)), point the third pointer to the next of second pointer, and take another temp pointer initialized to the last node i.e., pointer fourth.
- Iterate a loop while temp and third are not NULL, they are not equal and do not cross each other
- If the value of the sum is third?data + temp?data, then print the quadruple and increment the third pointer to the next of current third pointer and temp to the previous of current temp.
- If the value of sum is less than the third?data + temp?data, then increment the third pointer, i.e., third = third?next.
- Otherwise, decrement the temp pointer i.e temp = temp?prev.
- Move the second pointer to its next pointer.
- Move the first pointer to its next pointer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *next, *prev;
};
void insert( struct Node** head, int data)
{
struct Node* temp = new Node();
temp->data = data;
temp->next = temp->prev = NULL;
if ((*head) == NULL)
(*head) = temp;
else {
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
void PrintFourSum( struct Node* head, int x)
{
struct Node* first = head;
struct Node* second;
struct Node* third;
struct Node* fourth = head;
while (fourth->next != NULL) {
fourth = fourth->next;
}
struct Node* temp;
while (first != NULL
&& fourth != NULL
&& first != fourth
&& fourth->next != first) {
second = first->next;
while (second != NULL
&& fourth != NULL
&& second != fourth
&& fourth->next != second) {
int reqsum = x - (first->data
+ second->data);
third = second->next;
temp = fourth;
while (third != NULL && temp != NULL
&& third != temp
&& temp->next != third) {
int twosum = third->data
+ temp->data;
if (twosum == reqsum) {
cout << "(" << first->data
<< ", "
<< second->data
<< ", "
<< third->data
<< ", "
<< temp->data
<< ")\n" ;
third = third->next;
temp = temp->prev;
}
else if (twosum < reqsum) {
third = third->next;
}
else {
temp = temp->prev;
}
}
second = second->next;
}
first = first->next;
}
}
int main()
{
struct Node* head = NULL;
insert(&head, 2);
insert(&head, 1);
insert(&head, 0);
insert(&head, 0);
insert(&head, -1);
insert(&head, -2);
int X = 0;
PrintFourSum(head, X);
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next, prev;
};
static Node insert(Node head, int data)
{
Node temp = new Node();
temp.data = data;
temp.next = temp.prev = null ;
if (head == null )
(head) = temp;
else {
temp.next = head;
(head).prev = temp;
(head) = temp;
}
return temp;
}
static void PrintFourSum(Node head, int x)
{
Node first = head;
Node second = head;
Node third = head;
Node fourth = head;
while (fourth.next != null ) {
fourth = fourth.next;
}
Node temp;
while (first != null && fourth != null
&& first != fourth && fourth.next != first) {
second = first.next;
while (second != null && fourth != null
&& second != fourth
&& fourth.next != second) {
int reqsum = x - (first.data + second.data);
third = second.next;
temp = fourth;
while (third != null && temp != null
&& third != temp
&& temp.next != third) {
int twosum = third.data + temp.data;
if (twosum == reqsum) {
System.out.println(
"(" + first.data + ", "
+ second.data + ", "
+ third.data + ", " + temp.data
+ ")" );
third = third.next;
temp = temp.prev;
}
else if (twosum < reqsum) {
third = third.next;
}
else {
temp = temp.prev;
}
}
second = second.next;
}
first = first.next;
}
}
public static void main(String args[])
{
Node head = null ;
head = insert(head, 2 );
head = insert(head, 1 );
head = insert(head, 0 );
head = insert(head, 0 );
head = insert(head, - 1 );
head = insert(head, - 2 );
int x = 0 ;
PrintFourSum(head, x);
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self .left = None
self .right = None
def insert(head, data):
temp = Node(data)
temp.data = data
temp. next = temp.prev = None
if (head = = None ):
head = temp
else :
temp. next = head
head.prev = temp
head = temp
return head
def PrintFourSum(head, x):
first = head
second = None
third = None
fourth = head
while (fourth. next ! = None ):
fourth = fourth. next
temp = None
while (first ! = None and
fourth ! = None and
first ! = fourth and
fourth. next ! = first):
second = first. next
while (second ! = None and
fourth ! = None and
second ! = fourth and
fourth. next ! = second):
reqsum = x - (first.data +
second.data)
third = second. next
temp = fourth
while (third ! = None and temp ! = None and
third ! = temp and temp. next ! = third):
twosum = third.data + temp.data
if (twosum = = reqsum):
print ( "(" + str (first.data) +
", " + str (second.data) +
", " + str (third.data) +
", " + str (temp.data) + ")" )
third = third. next
temp = temp.prev
elif (twosum < reqsum):
third = third. next
else :
temp = temp.prev
second = second. next
first = first. next
if __name__ = = '__main__' :
head = None
head = insert(head, 2 )
head = insert(head, 1 )
head = insert(head, 0 )
head = insert(head, 0 )
head = insert(head, - 1 )
head = insert(head, - 2 )
X = 0
PrintFourSum(head, X)
|
C#
using System;
public class GFG {
public class Node {
public int data;
public Node next, prev;
};
static Node insert(Node head, int data) {
Node temp = new Node();
temp.data = data;
temp.next = temp.prev = null ;
if (head == null )
(head) = temp;
else {
temp.next = head;
(head).prev = temp;
(head) = temp;
}
return temp;
}
static void PrintFourSum(Node head, int x) {
Node first = head;
Node second = head;
Node third = head;
Node fourth = head;
while (fourth.next != null ) {
fourth = fourth.next;
}
Node temp;
while (first != null && fourth != null && first != fourth && fourth.next != first) {
second = first.next;
while (second != null && fourth != null && second != fourth && fourth.next != second) {
int reqsum = x - (first.data + second.data);
third = second.next;
temp = fourth;
while (third != null && temp != null && third != temp && temp.next != third) {
int twosum = third.data + temp.data;
if (twosum == reqsum) {
Console.WriteLine(
"(" + first.data + ", " + second.data + ", " + third.data + ", " + temp.data + ")" );
third = third.next;
temp = temp.prev;
}
else if (twosum < reqsum) {
third = third.next;
}
else {
temp = temp.prev;
}
}
second = second.next;
}
first = first.next;
}
}
public static void Main(String []args) {
Node head = null ;
head = insert(head, 2);
head = insert(head, 1);
head = insert(head, 0);
head = insert(head, 0);
head = insert(head, -1);
head = insert(head, -2);
int x = 0;
PrintFourSum(head, x);
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .next = null ;
this .prev = null ;
}
};
function insert(head, data)
{
var temp = new Node();
temp.data = data;
temp.next = temp.prev = null ;
if ((head) == null )
(head) = temp;
else {
temp.next = head;
(head).prev = temp;
(head) = temp;
}
return head;
}
function PrintFourSum(head, x)
{
var first = head;
var second;
var third;
var fourth = head;
while (fourth.next != null ) {
fourth = fourth.next;
}
var temp;
while (first != null
&& fourth != null
&& first != fourth
&& fourth.next != first) {
second = first.next;
while (second != null
&& fourth != null
&& second != fourth
&& fourth.next != second) {
var reqsum = x - (first.data
+ second.data);
third = second.next;
temp = fourth;
while (third != null && temp != null
&& third != temp
&& temp.next != third) {
var twosum = third.data
+ temp.data;
if (twosum == reqsum) {
document.write( "(" + first.data
+ ", "
+ second.data
+ ", "
+ third.data
+ ", "
+ temp.data
+ ")<br>" );
third = third.next;
temp = temp.prev;
}
else if (twosum < reqsum) {
third = third.next;
}
else {
temp = temp.prev;
}
}
second = second.next;
}
first = first.next;
}
}
var head = null ;
head = insert(head, 2);
head = insert(head, 1);
head = insert(head, 0);
head = insert(head, 0);
head = insert(head, -1);
head = insert(head, -2);
var X = 0;
PrintFourSum(head, X);
</script>
|
Output
(-2, -1, 1, 2)
(-2, 0, 0, 2)
(-1, 0, 0, 1)
Time Complexity: O(N3)
Auxiliary Space: O(1)