Find triplet sum closest to X in a sorted Doubly Linked List (DLL)
Last Updated :
09 Mar, 2022
Given a sorted doubly linked list of N nodes and an integer X, the task is to find the sum of three nodes in the list which is closest to X.
Examples:
Input: DLL: -8 ? 2 ? 3 ? 4 ? 5, X = 1
Output: 1
Explanation: The required three integers {-8, 4, 5} whose sum is 1 and is closest to 1.
Input: DLL: 1 ? 2 ? 3 ? 4, X = 3
Output: 6
Explanation: The required three integers are {1, 2, 3} whose sum is 6 and is closest to X = 3.
Naive Approach: The simplest approach to solve the given problem is to generate all possible triplets using three nested loops and then choose the triplet which has the sum closest to X and print the sum of the triplet.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use 3 pointer technique. Follow the steps below to solve this problem:
- Initialize 4 variables, first and second pointing to the head node of the doubly linked list, i.e., first = head, second = head, and tail and third both initialized to the last node of the doubly linked list.
- Initialize a variable diff, initialized as INT_MAX, which stores the closest sum to X.
- Iterate while the first node is not NULL and perform the following steps:
- Initialize second to the next of first, i.e., second = first?next and third = tail(the last node of doubly linked list).
- Iterate while second and third are not NULL and third is not equal to the second.
- Initialize a variable say, sum as (first?data + second?data + third?data).
- If the absolute value of X – sum is less than the absolute value of X – diff, then update the value of diff as sum.
- If sum is less than X, then increment second pointer, i.e., second = second?next.
- Otherwise, decrement third, i.e., third = third?prev.
- Move the first pointer to the next pointer, i.e., first = first?next.
- After completing the above steps, print the value of diff as the result.
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 closestTripletSum( struct Node* head, int X)
{
struct Node* first = head;
struct Node* second = head->next;
struct Node* tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
int diff = INT_MAX;
struct Node* third = tail;
while (first != NULL) {
second = first->next;
third = tail;
while (second != NULL && third != NULL
&& third != second) {
int sum = (first->data + second->data
+ third->data);
if ( abs (X - sum) < abs (X - diff)) {
diff = sum;
}
if (sum < X) {
second = second->next;
}
else {
third = third->prev;
}
}
first = first->next;
}
cout << diff;
}
int main()
{
struct Node* head = NULL;
insert(&head, 4);
insert(&head, 3);
insert(&head, 2);
insert(&head, 1);
int X = 3;
closestTripletSum(head, X);
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next, prev;
};
static Node head;
static void insert( 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;
}
}
static void closestTripletSum( int X)
{
Node first = head;
Node second = head.next;
Node tail = head;
while (tail.next != null ) {
tail = tail.next;
}
int diff = Integer.MAX_VALUE;
Node third = tail;
while (first != null ) {
second = first.next;
third = tail;
while (second != null && third != null
&& third != second) {
int sum = (first.data + second.data
+ third.data);
if (Math.abs(X - sum)
< Math.abs(X - diff)) {
diff = sum;
}
if (sum < X) {
second = second.next;
}
else {
third = third.prev;
}
}
first = first.next;
}
System.out.print(diff);
}
public static void main(String[] args)
{
head = null ;
insert( 4 );
insert( 3 );
insert( 2 );
insert( 1 );
int X = 3 ;
closestTripletSum(X);
}
}
|
Python3
import sys
class Node:
def __init__( self ):
self .data = 0 ;
self . next = None ;
self .prev = None ;
head = None ;
def insert(data):
temp = Node();
temp.data = data;
temp. next = temp.prev = None ;
global head;
if ((head) = = None ):
(head) = temp;
else :
temp. next = head;
(head).prev = temp;
(head) = temp;
def closestTripletSum(X):
first = head;
second = head. next ;
tail = head;
while (tail. next ! = None ):
tail = tail. next ;
diff = sys.maxsize;
third = tail;
while (first ! = None ):
second = first. next ;
third = tail;
while (second ! = None and third ! = None and third ! = second):
sum = (first.data + second.data + third.data);
if ( abs (X - sum ) < abs (X - diff)):
diff = sum ;
if ( sum < X):
second = second. next ;
else :
third = third.prev;
first = first. next ;
print (diff);
if __name__ = = '__main__' :
head = None ;
insert( 4 );
insert( 3 );
insert( 2 );
insert( 1 );
X = 3 ;
closestTripletSum(X);
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public class Node {
public int data;
public Node next, prev;
};
static Node head;
static void insert( 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;
}
}
static void closestTripletSum( int X) {
Node first = head;
Node second = head.next;
Node tail = head;
while (tail.next != null ) {
tail = tail.next;
}
int diff = int .MaxValue;
Node third = tail;
while (first != null ) {
second = first.next;
third = tail;
while (second != null && third != null && third != second) {
int sum = (first.data + second.data + third.data);
if (Math.Abs(X - sum) < Math.Abs(X - diff)) {
diff = sum;
}
if (sum < X) {
second = second.next;
} else {
third = third.prev;
}
}
first = first.next;
}
Console.Write(diff);
}
public static void Main(String[] args) {
head = null ;
insert(4);
insert(3);
insert(2);
insert(1);
int X = 3;
closestTripletSum(X);
}
}
|
Javascript
<script>
class Node {
constructor(val = 0) {
this .data = val;
this .prev = null ;
this .next = null ;
}
}
var head;
function insert(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;
}
}
function closestTripletSum(X)
{
var first = head;
var second = head.next;
var tail = head;
while (tail.next != null ) {
tail = tail.next;
}
var diff = Number.MAX_VALUE;
var third = tail;
while (first != null ) {
second = first.next;
third = tail;
while (second != null && third != null && third != second) {
var sum = (first.data + second.data + third.data);
if (Math.abs(X - sum) < Math.abs(X - diff)) {
diff = sum;
}
if (sum < X) {
second = second.next;
} else {
third = third.prev;
}
}
first = first.next;
}
document.write(diff);
}
head = null ;
insert(4);
insert(3);
insert(2);
insert(1);
var X = 3;
closestTripletSum(X);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...