JavaScript Program for Finding a Triplet from Three Linked Lists with a Sum Equal to a Given Number


In this article, we are going to implement a JavaScript program for finding a triplet from three linked lists with a sum equal to a given number. This problem is a kind of variation of the standard and famous three-sum problem but in a linked list manner. Let’s see the problem and implement its code along the key points of the problem.

Introduction to Problem

This problem is the variation of the standard problem of three sums where we are given three arrays and we have to find if there is any triplet present in the array with a sum exactly equal to the given number.

Here in this problem, we are given three linked lists and a number and we have to pick exactly one element from all the linked lists and the sum of the picked number must be equal to the given number.

If the sum of the numbers that are picked is equal to the given number, then we have to return yes otherwise false.

For example

If the given number is 10 and the linked lists are as follows −

List 1: 1 -> 2 -> 3 -> 4 -> 5
List 2: 3 -> 7 -> 8 -> 13
List 3: 9 -> 7 -> 2 -> 8

From the above given linked list, we can pick the following sets which have a sum equal to 10.

If we pick 1 from the first list, seven from the second list, and 2 from the last list.

Also, we can pick 2 from the last list, 3 from the second, and 5 from the first list.

So, in this case, we will return true.

Now imagine if the given number will be high like 25, then there is no set of three numbers present that will satisfy the condition.

Note − We must have to pick exactly one number from all three given lists.

Approach

We have seen the example of the problem, now let’s move to the steps to be followed for the implementation of the code −

Before moving to the main approach here are some assumptions that we are going to make are −

The second linked list will be in the sorted way and increasing order while the third linked list will be in the sorted way but in descending order because we are going to use the two-pointer technique which will be only applicable if the above assumption is true.

If the above-made assumption is not true then we have another method which is to make the linked list second in sorted order by using the merge sort technique which will take a total of O(N*log(N))) time complexity where N is the size of the linked list.

Similar to the third linked list we can sort that and reverse that to make it a strictly decreasing linked list that will also take O(N*log(N)) time complexity for size N.

Main approach −

Example

First, we will use the while loop to traverse over the linked list first and at each step we will use the two pointers approach to get the sum of all the current elements equals to given number.

// class for linked list node
class Node{
   constructor(data){
      this.value = data;
      this.next = null;
   }
}
 // function to find the triple or return false if the required number is not found 
function fun(lista,listb,listc,k){
   // creating temporary value of 
   var tempa = lista
   // Traverse all nodes of list A
   while (tempa != null)      {
      var tempb = listb
      var tempc = listc
      // Using two pointer approach for the listb and listc 
      while (tempb != null && tempc!=null)          {
         var current_sum = tempb.value + tempc.value + tempa.value;
         if (current_sum == k){
            console.log("Triplet found: " + tempa.value + " " + tempb.value + " " + tempc.value);
            return true;
         }
         // If the current sum is smaller then look for a greater value of b
         else if (current_sum < k)
         tempb = tempb.next;
         else
         tempc = tempc.next;
      }
      tempa = tempa.next;
   }
   console.log("No Triplet found in the given lists");
   return false;
}
// push function to create the linked list 
function push(head,data){
   let new_node = new Node(data);
   new_node.next = (head);
   (head) = new_node;
   return head;
}
// creating an unsorted linked list 
let head_A =null;
head_A = push (head_A, 2)
head_A = push (head_A, 4)
head_A = push (head_A, 1)
head_A = push (head_A, 8)
   
// create a sorted linked list b consisting of  4 10 15 20
let head_B = null;
head_B = push (head_B, 20)
head_B = push (head_B, 15)
head_B = push (head_B, 10)
head_B = push (head_B, 4)
   
// create another sorted list in descending order 10 9 4 2 
let head_C = null;
head_C = push (head_C, 2)
head_C = push (head_C, 4)
head_C = push (head_C, 9)
head_C = push (head_C, 10)
   
//given number 
var k = 25
// calling the function 
fun(head_A, head_B, head_C, k)

Time and Space Complexity

The time complexity of the above is O(N*N) where N is the size of the linked list because we are traversing over the first linked list that will cost use N iterations and at each iteration, we are applying a two-pointers approach that will cost us O(N).

We are not using any extra space which means the space complexity of the above code is O(1).

Conclusion

In this article, we have implemented a JavaScript program for finding a triplet from three linked lists with a sum equal to a given number. This problem is a kind of variation of the standard and famous three-sum problem but in a linked list manner. The time complexity of the above is O(N*N) where N is the size of the linked list and the space complexity is O(1).

Updated on: 24-Mar-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements