GeeksforGeeks » Linked List specific questions
Find 0 sum elements in three Linked lists
(9 posts)-
Given three lists: A, B and C of length n each. Write a function which returns true if any 3 three numbers (1 from each list), sum up to zero. Else return false, if there is no such combination.
-
The simple solution is to have 3 loops, one loop for each linked list. Check for the 0 sum in the innermost loop and if sum becomes 0 print the elements.
Time Complexity O(n^3)
-
Here is another thought
Make a Hash Map for sum of all the elements of first two Linked Lists. Now traverse the third list, for each element M (of third list), look for -M in Hash Map, if found return true.
-
Sort three linked list separately 3nlogn.
Sum ptr1,ptr2,ptr3. If sum <0 . increment smallest pointer. else if sum > 0 decrement greatest pointer. else print ptr1,ptr2,ptr3.
Note: We have to see how decrement works in linked list, (either by remembering prev pointers.).
-
Hi Inba ,
Tyr the above logicon following sorted Lists....A( -1,-2,-3,-4,-5)
B(-6,-7,-8,-9,-10)
C(01,1,2,7,9)I think It fails.
-
another optimization for o(n3) is to split the function
a+b+c=0 as a+b=-c
with O(n^2) generate two vectors, do a binary search for one array in another , this reduces O(n^3) to O(n^2logn)
-
@Inba
Your approach saves time for most cases. But still the worst case complexity is O(N^3).
-
karthikeyan's approach seems to be one viable option with good optimization. Has anyone found a better answer?
Thanks, -
Naive Solution Code:
#include<stdio.h> struct node { int value; struct node *next; }; struct node *h1 = NULL, *h2 = NULL, *h3 = NULL; struct node *create_new_node(int value) { struct node *temp = (struct node *)malloc(sizeof(struct node)); temp->value = value; temp->next = NULL; return temp; } void insert_at_head(struct node *n, struct node *head) { struct node *temp = head->next; head->next = n; n->next = temp; } void print_list(struct node *head) { printf("Head: %d\n",head->value); struct node *temp = head->next; while(temp != NULL) { printf("%d ",temp->value); temp = temp->next; } printf("\n"); } const char *fun_mode(struct node *head1, struct node *head2, struct node *head3) { //returns true if sum of any two equals the third in 3 lists struct node *p = head1; struct node *q = head2; struct node *r = head3; while(p!=NULL){ //printf("Value of P: %d\n",p->value); q = head2; while(q!=NULL){ //printf("\tValue of Q: %d\n",q->value); int d = p->value + q->value; r = head3; while(r!=NULL){ //printf("\t\tValue of R: %d\n",r->value); int sum = d + r->value; if(sum == 0) { //printf("\n%d+%d+%d = 0\n",p->value,q->value,r->value); return "true"; } else r=r->next; } q=q->next; } p=p->next; } return "false"; } int main() { h1 = create_new_node(0); h2 = create_new_node(1); h3 = create_new_node(2); insert_at_head(create_new_node(1),h1); insert_at_head(create_new_node(2),h1); insert_at_head(create_new_node(3),h1); insert_at_head(create_new_node(4),h1); insert_at_head(create_new_node(5),h1); insert_at_head(create_new_node(1),h2); insert_at_head(create_new_node(2),h2); insert_at_head(create_new_node(3),h2); insert_at_head(create_new_node(4),h2); insert_at_head(create_new_node(5),h2); insert_at_head(create_new_node(1),h3); insert_at_head(create_new_node(2),h3); insert_at_head(create_new_node(-10),h3); insert_at_head(create_new_node(2),h3); print_list(h1); print_list(h2); print_list(h3); const char *ans = fun_mode(h1,h2,h3); printf("\n%s",ans); }
Reply
You must log in to post.