Count triplets in a sorted doubly linked list whose sum is equal to a given value x
Given a sorted doubly linked list of distinct nodes(no two nodes have the same data) and a value x. Count triplets in the list that sum up to a given value x.
Examples:
Method 1 (Naive Approach):
Using three nested loops generate all triplets and check whether elements in the triplet sum up to x or not.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next, *prev;
};
int countTriplets( struct Node* head, int x)
{
struct Node* ptr1, *ptr2, *ptr3;
int count = 0;
for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
for (ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next)
for (ptr3 = ptr2->next; ptr3 != NULL; ptr3 = ptr3->next)
if ((ptr1->data + ptr2->data + ptr3->data) == x)
count++;
return count;
}
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;
}
}
int main()
{
struct Node* head = NULL;
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 17;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Node
{
int data;
Node prev, next;
Node( int val)
{
data = val;
prev = null ;
next = null ;
}
}
class GFG
{
static int countTriplets(Node head, int x)
{
Node ptr1, ptr2, ptr3;
int count = 0 ;
for (ptr1 = head; ptr1 != null ; ptr1 = ptr1.next)
for (ptr2 = ptr1.next; ptr2 != null ; ptr2 = ptr2.next)
for (ptr3 = ptr2.next; ptr3 != null ; ptr3 = ptr3.next)
if ((ptr1.data + ptr2.data + ptr3.data) == x)
count++;
return count;
}
static Node insert(Node head, int val)
{
Node temp = new Node(val);
if (head == null )
head = temp;
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
public static void main(String args[])
{
Node head = null ;
head = insert(head, 9 );
head = insert(head, 8 );
head = insert(head, 6 );
head = insert(head, 5 );
head = insert(head, 4 );
head = insert(head, 2 );
head = insert(head, 1 );
int x = 17 ;
System.out.println( "count = " + countTriplets(head, x));
}
}
|
Python3
class Node:
def __init__( self ):
self .data = None
self .prev = None
self . next = None
def countTriplets( head, x):
ptr1 = head
ptr2 = None
ptr3 = None
count = 0
while (ptr1 ! = None ):
ptr2 = ptr1. next
while ( ptr2 ! = None ):
ptr3 = ptr2. next
while ( ptr3 ! = None ):
if ((ptr1.data + ptr2.data + ptr3.data) = = x):
count = count + 1
ptr3 = ptr3. next
ptr2 = ptr2. next
ptr1 = ptr1. next
return count
def insert(head, data):
temp = Node()
temp.data = data
temp. next = temp.prev = None
if ((head) = = None ):
(head) = temp
else :
temp. next = head
(head).prev = temp
(head) = temp
return head
head = None
head = insert(head, 9 )
head = insert(head, 8 )
head = insert(head, 6 )
head = insert(head, 5 )
head = insert(head, 4 )
head = insert(head, 2 )
head = insert(head, 1 )
x = 17
print ( "Count = " , countTriplets(head, x))
|
C#
using System;
public class Node
{
public int data;
public Node prev, next;
public Node( int val)
{
data = val;
prev = null ;
next = null ;
}
}
class GFG
{
static int countTriplets(Node head, int x)
{
Node ptr1, ptr2, ptr3;
int count = 0;
for (ptr1 = head; ptr1 != null ; ptr1 = ptr1.next)
for (ptr2 = ptr1.next; ptr2 != null ; ptr2 = ptr2.next)
for (ptr3 = ptr2.next; ptr3 != null ; ptr3 = ptr3.next)
if ((ptr1.data + ptr2.data + ptr3.data) == x)
count++;
return count;
}
static Node insert(Node head, int val)
{
Node temp = new Node(val);
if (head == null )
head = temp;
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
public static void Main(String []args)
{
Node head = null ;
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 17;
Console.WriteLine( "count = " + countTriplets(head, x));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .prev = null ;
this .next = null ;
}
}
function countTriplets( head , x) {
var ptr1, ptr2, ptr3;
var count = 0;
for (ptr1 = head; ptr1 != null ; ptr1 = ptr1.next)
for (ptr2 = ptr1.next; ptr2 != null ; ptr2 = ptr2.next)
for (ptr3 = ptr2.next; ptr3 != null ; ptr3 = ptr3.next)
if ((ptr1.data + ptr2.data + ptr3.data) == x)
count++;
return count;
}
function insert( head , val) {
temp = new Node(val);
if (head == null )
head = temp;
else {
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
head = null ;
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
var x = 17;
document.write( "count = " + countTriplets(head, x));
</script>
|
Output:
Count = 2
Time Complexity: O(n3)
Auxiliary Space: O(1)
Method 2 (Hashing):
Create a hash table with (key, value) tuples represented as (node data, node pointer) tuples. Traverse the doubly linked list and store each node’s data and its pointer pair(tuple) in the hash table. Now, generate each possible pair of nodes. For each pair of nodes, calculate the p_sum(sum of data in the two nodes) and check whether (x-p_sum) exists in the hash table or not. If it exists, then also verify that the two nodes in the pair are not same to the node associated with (x-p_sum) in the hash table and finally increment count. Return (count / 3) as each triplet is counted 3 times in the above process.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next, *prev;
};
int countTriplets( struct Node* head, int x)
{
struct Node* ptr, *ptr1, *ptr2;
int count = 0;
unordered_map< int , Node*> um;
for (ptr = head; ptr != NULL; ptr = ptr->next)
um[ptr->data] = ptr;
for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
for (ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next) {
int p_sum = ptr1->data + ptr2->data;
if (um.find(x - p_sum) != um.end() && um[x - p_sum] != ptr1
&& um[x - p_sum] != ptr2)
count++;
}
return (count / 3);
}
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;
}
}
int main()
{
struct Node* head = NULL;
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 17;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node next, prev;
Node( int val)
{
data = val;
prev = null ;
next = null ;
}
};
static int countTriplets(Node head, int x)
{
Node ptr, ptr1, ptr2;
int count = 0 ;
HashMap<Integer,Node> um = new HashMap<Integer,Node>();
for (ptr = head; ptr != null ; ptr = ptr.next)
um.put(ptr.data, ptr);
for (ptr1 = head; ptr1 != null ; ptr1 = ptr1.next)
for (ptr2 = ptr1.next; ptr2 != null ; ptr2 = ptr2.next) {
int p_sum = ptr1.data + ptr2.data;
if (um.containsKey(x - p_sum) && um.get(x - p_sum) != ptr1
&& um.get(x - p_sum) != ptr2)
count++;
}
return (count / 3 );
}
static Node insert(Node head, int val)
{
Node temp = new Node(val);
if (head == null )
head = temp;
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
public static void main(String[] args)
{
Node head = null ;
head = insert(head, 9 );
head = insert(head, 8 );
head = insert(head, 6 );
head = insert(head, 5 );
head = insert(head, 4 );
head = insert(head, 2 );
head = insert(head, 1 );
int x = 17 ;
System.out.print( "Count = "
+ countTriplets(head, x));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
self .prev = None
def countTriplets(head, x):
ptr2 = head
count = 0 ;
um = dict ()
ptr = head
while ptr! = None :
um[ptr.data] = ptr;
ptr = ptr. next
ptr1 = head
while ptr1! = None :
ptr2 = ptr1. next
while ptr2! = None :
p_sum = ptr1.data + ptr2.data;
if ((x - p_sum) in um) and um[x - p_sum] ! = ptr1 and um[x - p_sum] ! = ptr2:
count + = 1
ptr2 = ptr2. next
ptr1 = ptr1. next
return (count / / 3 );
def insert(head, data):
temp = Node(data);
if ((head) = = None ):
(head) = temp;
else :
temp. next = head;
(head).prev = temp;
(head) = temp;
return head
if __name__ = = '__main__' :
head = None ;
head = insert(head, 9 );
head = insert(head, 8 );
head = insert(head, 6 );
head = insert(head, 5 );
head = insert(head, 4 );
head = insert(head, 2 );
head = insert( head, 1 );
x = 17 ;
print ( "Count = " + str (countTriplets(head, x)))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class Node {
public int data;
public Node next, prev;
public Node( int val)
{
data = val;
prev = null ;
next = null ;
}
};
static int countTriplets(Node head, int x)
{
Node ptr, ptr1, ptr2;
int count = 0;
Dictionary< int ,Node> um = new Dictionary< int ,Node>();
for (ptr = head; ptr != null ; ptr = ptr.next)
if (um.ContainsKey(ptr.data))
um[ptr.data] = ptr;
else
um.Add(ptr.data, ptr);
for (ptr1 = head; ptr1 != null ; ptr1 = ptr1.next)
for (ptr2 = ptr1.next; ptr2 != null ; ptr2 = ptr2.next)
{
int p_sum = ptr1.data + ptr2.data;
if (um.ContainsKey(x - p_sum) && um[x - p_sum] != ptr1
&& um[x - p_sum] != ptr2)
count++;
}
return (count / 3);
}
static Node insert(Node head, int val)
{
Node temp = new Node(val);
if (head == null )
head = temp;
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
public static void Main(String[] args)
{
Node head = null ;
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 17;
Console.Write( "Count = "
+ countTriplets(head, x));
}
}
|
Javascript
<script>
class Node{
constructor(data)
{
this .data = data;
this .prev = null ;
this .next = null ;
}
}
function countTriplets(head, x)
{
let ptr, ptr1, ptr2;
let count = 0;
let um = new Map();
for (ptr = head; ptr != null ; ptr = ptr.next)
um.set(ptr.data, ptr);
for (ptr1 = head;
ptr1 != null ;
ptr1 = ptr1.next)
for (ptr2 = ptr1.next;
ptr2 != null ;
ptr2 = ptr2.next)
{
let p_sum = ptr1.data + ptr2.data;
if (um.has(x - p_sum) &&
um.get(x - p_sum) != ptr1 &&
um.get(x - p_sum) != ptr2)
count++;
}
return (count / 3);
}
function insert(head, val)
{
let temp = new Node(val);
if (head == null )
head = temp;
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
let head = null ;
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
let x = 17;
document.write( "Count = " +
countTriplets(head, x));
</script>
|
Output:
Count = 2
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method 3 Efficient Approach(Use of two pointers):
Traverse the doubly linked list from left to right. For each current node during the traversal, initialize two pointers first = pointer to the node next to the current node and last = pointer to the last node of the list. Now, count pairs in the list from first to last pointer that sum up to value (x – current node’s data) (algorithm described in this post). Add this count to the total_count of triplets. Pointer to the last node can be found only once in the beginning.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next, *prev;
};
int countPairs( struct Node* first, struct Node* second, int value)
{
int count = 0;
while (first != NULL && second != NULL &&
first != second && second->next != first) {
if ((first->data + second->data) == value) {
count++;
first = first->next;
second = second->prev;
}
else if ((first->data + second->data) > value)
second = second->prev;
else
first = first->next;
}
return count;
}
int countTriplets( struct Node* head, int x)
{
if (head == NULL)
return 0;
struct Node* current, *first, *last;
int count = 0;
last = head;
while (last->next != NULL)
last = last->next;
for (current = head; current != NULL; current = current->next) {
first = current->next;
count += countPairs(first, last, x - current->data);
}
return count;
}
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;
}
}
int main()
{
struct Node* head = NULL;
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 17;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node next, prev;
};
static int countPairs(Node first, Node second, int value)
{
int count = 0 ;
while (first != null && second != null &&
first != second && second.next != first) {
if ((first.data + second.data) == value) {
count++;
first = first.next;
second = second.prev;
}
else if ((first.data + second.data) > value)
second = second.prev;
else
first = first.next;
}
return count;
}
static int countTriplets(Node head, int x)
{
if (head == null )
return 0 ;
Node current, first, last;
int count = 0 ;
last = head;
while (last.next != null )
last = last.next;
for (current = head; current != null ; current = current.next) {
first = current.next;
count += countPairs(first, last, x - current.data);
}
return count;
}
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 head;
}
public static void main(String[] args)
{
Node head = null ;
head = insert(head, 9 );
head = insert(head, 8 );
head = insert(head, 6 );
head = insert(head, 5 );
head = insert(head, 4 );
head = insert(head, 2 );
head = insert(head, 1 );
int x = 17 ;
System.out.print( "Count = "
+ countTriplets(head, x));
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self . next = None
self .prev = None
def countPairs(first, second, value):
count = 0
while (first ! = None and second ! = None and
first ! = second and second. next ! = first):
if ((first.data + second.data) = = value):
count + = 1
first = first. next
second = second.prev
elif ((first.data + second.data) > value):
second = second.prev
else :
first = first. next
return count
def countTriplets(head, x):
if (head = = None ):
return 0
current, first, last = head, None , None
count = 0
last = head
while (last. next ! = None ):
last = last. next
while current ! = None :
first = current. next
count, current = count + countPairs(
first, last, x - current.data), current. next
return count
def insert(head, data):
temp = Node(data)
if (head = = None ):
head = temp
else :
temp. next = head
head.prev = temp
head = temp
return head
if __name__ = = '__main__' :
head = None
head = insert(head, 9 )
head = insert(head, 8 )
head = insert(head, 6 )
head = insert(head, 5 )
head = insert(head, 4 )
head = insert(head, 2 )
head = insert(head, 1 )
x = 17
print ( "Count = " , countTriplets(head, x))
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next, prev;
};
static int countPairs(Node first, Node second, int value)
{
int count = 0;
while (first != null && second != null &&
first != second && second.next != first) {
if ((first.data + second.data) == value) {
count++;
first = first.next;
second = second.prev;
}
else if ((first.data + second.data) > value)
second = second.prev;
else
first = first.next;
}
return count;
}
static int countTriplets(Node head, int x)
{
if (head == null )
return 0;
Node current, first, last;
int count = 0;
last = head;
while (last.next != null )
last = last.next;
for (current = head; current != null ; current = current.next) {
first = current.next;
count += countPairs(first, last, x - current.data);
}
return count;
}
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 head;
}
public static void Main(String[] args)
{
Node head = null ;
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 17;
Console.Write( "Count = "
+ countTriplets(head, x));
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .next = this .prev = null ;
}
}
function countPairs(first, second, value)
{
let count = 0;
while (first != null && second != null &&
first != second && second.next != first)
{
if ((first.data + second.data) == value)
{
count++;
first = first.next;
second = second.prev;
}
else if ((first.data + second.data) > value)
second = second.prev;
else
first = first.next;
}
return count;
}
function countTriplets(head, x)
{
if (head == null )
return 0;
let current, first, last;
let count = 0;
last = head;
while (last.next != null )
last = last.next;
for (current = head;
current != null ;
current = current.next)
{
first = current.next;
count += countPairs(first, last,
x - current.data);
}
return count;
}
function insert(head, data)
{
let 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;
}
let head = null ;
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
let x = 17;
document.write( "Count = " +
countTriplets(head, x));
</script>
|
Output:
Count = 2
Time Complexity: O(n2)
Auxiliary Space: O(1)
Another Solution
find tripple sum in Doubly linkedList.
C++
#include <iostream>
#include <vector>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
Node( int x)
{
data = x;
next = nullptr;
prev = nullptr;
}
};
class Solution
{
public :
vector<vector< int >> trippleSumInLinkedList(Node *head, int sumv)
{
vector<vector< int >> res;
Node *s, *m, *e;
s = head;
m = head;
e = head;
while (e->next != nullptr)
e = e->next;
while (s->next->next != nullptr)
{
int currSum = sumv - s->data;
m = s->next;
Node *ev = e;
while (m != nullptr && ev != nullptr && m != ev)
{
int newSum = m->data + ev->data;
if (newSum == currSum)
{
res.push_back({s->data, m->data, ev->data});
m = m->next;
}
else if (newSum > currSum)
ev = ev->prev;
else
m = m->next;
}
s = s->next;
}
return res;
}
};
int main()
{
Node *head = new Node(1);
Node *node2 = new Node(2);
Node *node3 = new Node(4);
Node *node4 = new Node(5);
Node *node5 = new Node(6);
Node *node6 = new Node(8);
Node *node7 = new Node(9);
head->next = node2;
node2->prev = head;
node2->next = node3;
node3->prev = node2;
node3->next = node4;
node4->next = node5;
node4->prev = node3;
node5->prev = node4;
node5->next = node6;
node6->prev = node5;
node6->next = node7;
node7->prev = node6;
Solution sol;
vector<vector< int >> res = sol.trippleSumInLinkedList(head, 15);
for ( int i = 0; i < res.size(); i++)
cout << res[i][0] << ", " << res[i][1] << ", " << res[i][2] << endl;
return 0;
}
|
Java
import java.util.ArrayList;
class Node {
public int data;
public Node next;
public Node prev;
public Node( int x) {
data = x;
next = null ;
prev = null ;
}
}
public class Solution {
public ArrayList<ArrayList<Integer>> trippleSumInLinkedList(Node head, int sumv) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
Node s, m, e;
s = head;
m = head;
e = head;
while (e.next != null )
e = e.next;
while (s.next.next != null ) {
int currSum = sumv - s.data;
m = s.next;
Node ev = e;
while (m != null && ev != null && m != ev) {
int newSum = m.data + ev.data;
if (newSum == currSum) {
ArrayList<Integer> triple = new ArrayList<Integer>();
triple.add(s.data);
triple.add(m.data);
triple.add(ev.data);
res.add(triple);
m = m.next;
}
else if (newSum > currSum)
ev = ev.prev;
else
m = m.next;
}
s = s.next;
}
return res;
}
public static void main(String[] args) {
Node head = new Node( 1 );
Node node2 = new Node( 2 );
Node node3 = new Node( 4 );
Node node4 = new Node( 5 );
Node node5 = new Node( 6 );
Node node6 = new Node( 8 );
Node node7 = new Node( 9 );
head.next = node2;
node2.prev = head;
node2.next = node3;
node3.prev = node2;
node3.next = node4;
node4.next = node5;
node4.prev = node3;
node5.prev = node4;
node5.next = node6;
node6.prev = node5;
node6.next = node7;
node7.prev = node6;
Solution sol = new Solution();
ArrayList<ArrayList<Integer>> res = sol.trippleSumInLinkedList(head, 15 );
for ( int i = 0 ; i < res.size(); i++)
System.out.println(res.get(i).get( 0 ) + ", " + res.get(i).get( 1 ) + ", " + res.get(i).get( 2 ));
}
}
|
Python3
class Solution:
def trippleSumInLinkedList( self , head,sumv):
res = []
s,m,e = head,head,head
while e. next ! = None :
e = e. next
while s. next . next ! = None :
currSum = sumv - s.data
m = s. next
ev = e
while m and ev and m ! = ev:
newSum = m.data + ev.data
if newSum = = currSum:
res.append([s.data,m.data,ev.data])
m = m. next
elif newSum > currSum:
ev = ev.prev
else :
m = m. next
s = s. next
return res
sol = Solution()
head = Node( 1 )
node2 = Node( 2 )
node3 = Node( 4 )
node4 = Node( 5 )
node5 = Node( 6 )
node6 = Node( 8 )
node7 = Node( 9 )
head. next = node2
node2.prev = head
node2. next = node3
node3.prev = node2
node3. next = node4
node4. next = node5
node4.prev = node3
node5.prev = node4
node5. next = node6
node6.prev = node5
node6. next = node7
node7.prev = node6
print ( 'solution: ' ,sol.trippleSumInLinkedList(head, 15 ))
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node next;
public Node prev;
public Node( int x)
{
data = x;
next = null ;
prev = null ;
}
}
class Solution {
public List<List< int > >
trippleSumInLinkedList(Node head, int sumv)
{
List<List< int > > res = new List<List< int > >();
Node s, m, e;
s = head;
m = head;
e = head;
while (e.next != null )
e = e.next;
while (s.next.next != null ) {
int currSum = sumv - s.data;
m = s.next;
Node ev = e;
while (m != null && ev != null && m != ev) {
int newSum = m.data + ev.data;
if (newSum == currSum) {
res.Add( new List< int >{ s.data, m.data,
ev.data });
m = m.next;
}
else if (newSum > currSum)
ev = ev.prev;
else
m = m.next;
}
s = s.next;
}
return res;
}
}
class Program {
static void Main( string [] args)
{
Node head = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(4);
Node node4 = new Node(5);
Node node5 = new Node(6);
Node node6 = new Node(8);
Node node7 = new Node(9);
head.next = node2;
node2.prev = head;
node2.next = node3;
node3.prev = node2;
node3.next = node4;
node4.next = node5;
node4.prev = node3;
node5.prev = node4;
node5.next = node6;
node6.prev = node5;
node6.next = node7;
node7.prev = node6;
Solution sol = new Solution();
List<List< int > > res
= sol.trippleSumInLinkedList(head, 15);
foreach (List< int > triple in res)
{
Console.WriteLine( "{0}, {1}, {2}" , triple[0],
triple[1], triple[2]);
}
Console.ReadKey();
}
}
|
Javascript
class Node {
constructor(x) {
this .data = x;
this .next = null ;
this .prev = null ;
}
}
class Solution {
trippleSumInLinkedList(head, sumv) {
let res = [];
let s, m, e;
s = head;
m = head;
e = head;
while (e.next != null )
e = e.next;
while (s.next.next != null ) {
let currSum = sumv - s.data;
m = s.next;
let ev = e;
while (m != null && ev != null && m != ev) {
let newSum = m.data + ev.data;
if (newSum == currSum) {
res.push([s.data, m.data, ev.data]);
m = m.next;
} else if (newSum > currSum)
ev = ev.prev;
else
m = m.next;
}
s = s.next;
}
return res;
}
}
let head = new Node(1);
let node2 = new Node(2);
let node3 = new Node(4);
let node4 = new Node(5);
let node5 = new Node(6);
let node6 = new Node(8);
let node7 = new Node(9);
head.next = node2;
node2.prev = head;
node2.next = node3;
node3.prev = node2;
node3.next = node4;
node4.next = node5;
node4.prev = node3;
node5.prev = node4;
node5.next = node6;
node6.prev = node5;
node6.next = node7;
node7.prev = node6;
let sol = new Solution();
let res = sol.trippleSumInLinkedList(head, 15);
for (let i in res)
console.log(res[i][0] + ", " + res[i][1] + ", " + res[i][2]);
|
Output
1, 5, 9
1, 6, 8
2, 4, 9
2, 5, 8
4, 5, 6
Time complexity : O(n^2), where n is the length of the linked list.
Space complexity :O(1),because it only uses a constant amount of extra memory to store pointers to the nodes being examined
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...