ronith
7/23/2018 - 3:14 AM

Find a triplet from three linked lists with sum equal to a given number

Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8 and 90->20->59, and the given number is 101, the output should be tripel “6 5 90”.

In the following solutions, size of all three linked lists is assumed same for simplicity of analysis. The following solutions work for linked lists of different sizes also.

A simple method to solve this problem is to run three nested loops. The outermost loop picks an element from list a, the middle loop picks an element from b and the innermost loop picks from c. The innermost loop also checks whether the sum of values of current nodes of a, b and c is equal to given number. The time complexity of this method will be O(n^3).

Sorting can be used to reduce the time complexity to O(n*n). Following are the detailed steps.

  1. Sort list b in ascending order, and list c in descending order.
  2. After the b and c are sorted, one by one pick an element from list a and find the pair by traversing both b and c.
// https://www.geeksforgeeks.org/find-a-triplet-from-three-linked-lists-with-sum-equal-to-a-given-number/
#include <iostream>
#include <list>
using namespace std;

struct linked_list {
    int data;
    struct linked_list *next;
};
typedef struct linked_list node;

void print (node* n) {
    while(n) {
        cout<< n->data<< "->";
        n = n->next;
    }
    cout<< "NULL";
}

void insert(node **headref, int n) {
    node *last = *headref;
    node* temp = new node;
    temp->data = n;
    temp->next = NULL;
    if (last == NULL) {
        *headref = temp;
        return;
    }
    while (last->next)
        last = last->next;
    last->next = temp;
    return;
}

void triplet (node* head1, node* head2, node* head3,int n) {
    node *temp1 = head1;
    while (temp1) {
        node *temp2=head2,*temp3=head3;
        while (temp2&&temp3) {
            if (temp1->data + temp2->data + temp3->data == n) {
                cout<< "Triplet are :" << temp1->data << " " << temp2->data << " " << temp3->data << "\n";
                temp2=temp2->next;
                temp3=temp3->next;
                }
            else if (temp1->data + temp2->data + temp3->data < n)
                temp2=temp2->next;
            else
                temp3=temp3->next;
        }
        temp1=temp1->next;
    }
}

int main() {
    int n; //input such that LL1 can be in any order, LL2 has to be in ascending order, LL3 has to be in descending order.
    node* head1=NULL, *head2=NULL,*head3=NULL;
    cout<< "Enter 1st linked list:\n";
    while (true) {
        cout<< "Enter the number: ";
        cin>>n;
        if (n==-9)
            break;
        else
            insert(&head1, n);
    }
    cout<< "Enter 2nd linked list:\n";
    while (true) {
        cout<< "Enter the number: ";
        cin>>n;
        if (n==-9)
            break;
        else
            insert(&head2, n);
    }
    cout<< "Enter 3rd linked list:\n";
    while (true) {
        cout<< "Enter the number: ";
        cin>>n;
        if (n==-9)
            break;
        else
            insert(&head3, n);
    }
    cout<< "Enter the sum: ";
    cin>>n;
    triplet(head1,head2,head3,n);
}