Given two linked lists(can be sorted or unsorted) of size n1 and n2 of distinct elements. Given a value x. The problem is to count all pairs from both lists whose sum is equal to the given value x.
Note: The pair has an element from each linked list.
Examples:
Input : list1 = 3->1->5->7
list2 = 8->2->5->3
x = 10
Output : 2
The pairs are:
(5, 5) and (7, 3)
Input : list1 = 4->3->5->7->11->2->1
list2 = 2->3->4->5->6->8-12
x = 9
Output : 5
Method 1 (Naive Approach): Using two loops pick elements from both the linked lists and check whether the sum of the pair is equal to x or not.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int countPairs( struct Node* head1, struct Node* head2, int x)
{
int count = 0;
struct Node *p1, *p2;
for (p1 = head1; p1 != NULL; p1 = p1->next)
for (p2 = head2; p2 != NULL; p2 = p2->next)
if ((p1->data + p2->data) == x)
count++;
return count;
}
int main()
{
struct Node* head1 = NULL;
struct Node* head2 = NULL;
push(&head1, 7);
push(&head1, 5);
push(&head1, 1);
push(&head1, 3);
push(&head2, 3);
push(&head2, 5);
push(&head2, 2);
push(&head2, 8);
int x = 10;
cout << "Count = "
<< countPairs(head1, head2, x);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
class GFG
{
static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2, int x)
{
int count = 0 ;
Iterator<Integer> itr1 = head1.iterator();
while (itr1.hasNext())
{
Iterator<Integer> itr2 = head2.iterator();
Integer t = itr1.next();
while (itr2.hasNext())
{
if ((t + itr2.next()) == x)
count++;
}
}
return count;
}
public static void main(String[] args)
{
Integer arr1[] = { 3 , 1 , 5 , 7 };
Integer arr2[] = { 8 , 2 , 5 , 3 };
LinkedList<Integer> head1 = new LinkedList<>(Arrays.asList(arr1));
LinkedList<Integer> head2 = new LinkedList<>(Arrays.asList(arr2));
int x = 10 ;
System.out.println( "Count = " + countPairs(head1, head2, x));
}
}
|
Python3
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def push(head_ref,new_data):
new_node = Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def countPairs(head1, head2, x):
count = 0
p1 = head1
while (p1 ! = None ):
p2 = head2
while (p2 ! = None ):
if ((p1.data + p2.data) = = x):
count + = 1
p2 = p2. next
p1 = p1. next
return count
if __name__ = = '__main__' :
head1 = None
head2 = None
head1 = push(head1, 7 )
head1 = push(head1, 5 )
head1 = push(head1, 1 )
head1 = push(head1, 3 )
head2 = push(head2, 3 )
head2 = push(head2, 5 )
head2 = push(head2, 2 )
head2 = push(head2, 8 )
x = 10
print ( "Count = " ,countPairs(head1, head2, x))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPairs(List< int > head1, List< int > head2, int x)
{
int count = 0;
foreach ( int itr1 in head1)
{
int t = itr1;
foreach ( int itr2 in head2)
{
if ((t + itr2) == x)
count++;
}
}
return count;
}
public static void Main(String []args)
{
int []arr1 = {3, 1, 5, 7};
int []arr2 = {8, 2, 5, 3};
List< int > head1 = new List< int >(arr1);
List< int > head2 = new List< int >(arr2);
int x = 10;
Console.WriteLine( "Count = " + countPairs(head1, head2, x));
}
}
|
Javascript
<script>
function countPairs( head1, head2 , x) {
var count = 0;
for ( var itr1 of head1) {
for ( var itr2 of head2) {
if ((itr1 + itr2) == x)
count++;
}
}
return count;
}
var arr1 = [ 3, 1, 5, 7 ];
var arr2 = [ 8, 2, 5, 3 ];
var head1 = (arr1);
var head2 = arr2;
var x = 10;
document.write( "Count = " + countPairs(head1, head2, x));
</script>
|
Time Complexity: O(n1*n2)
Auxiliary Space: O(1)
Method 2 (Sorting): Sort the 1st linked list in ascending order and the 2nd linked list in descending order using merge sort technique. Now traverse both the lists from left to right in the following way:
Algorithm:
countPairs(list1, list2, x)
Initialize count = 0
while list1 != NULL and list2 != NULL
if (list1->data + list2->data) == x
list1 = list1->next
list2 = list2->next
count++
else if (list1->data + list2->data) > x
list2 = list2->next
else
list1 = list1->next
return count
For simplicity, the implementation given below assumes that list1 is sorted in ascending order and list2 is sorted in descending order.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int countPairs( struct Node* head1, struct Node* head2,
int x)
{
int count = 0;
while (head1 != NULL && head2 != NULL)
{
if ((head1->data + head2->data) == x)
{
head1 = head1->next;
head2 = head2->next;
count++;
}
else if ((head1->data + head2->data) > x)
head2 = head2->next;
else
head1 = head1->next;
}
return count;
}
int main()
{
struct Node* head1 = NULL;
struct Node* head2 = NULL;
push(&head1, 7);
push(&head1, 5);
push(&head1, 3);
push(&head1, 1);
push(&head2, 2);
push(&head2, 3);
push(&head2, 5);
push(&head2, 8);
int x = 10;
cout << "Count = "
<< countPairs(head1, head2, x);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
class GFG
{
static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2, int x)
{
int count = 0 ;
Collections.sort(head1);
Collections.sort(head2,Collections.reverseOrder());
Iterator<Integer> itr1 = head1.iterator();
Iterator<Integer> itr2 = head2.iterator();
Integer num1 = itr1.hasNext() ? itr1.next() : null ;
Integer num2 = itr2.hasNext() ? itr2.next() : null ;
while (num1 != null && num2 != null )
{
if ((num1 + num2) == x)
{
num1 = itr1.hasNext() ? itr1.next() : null ;
num2 = itr2.hasNext() ? itr2.next() : null ;
count++;
}
else if ((num1 + num2) > x)
num2 = itr2.hasNext() ? itr2.next() : null ;
else
num1 = itr1.hasNext() ? itr1.next() : null ;
}
return count;
}
public static void main(String[] args)
{
Integer arr1[] = { 3 , 1 , 5 , 7 };
Integer arr2[] = { 8 , 2 , 5 , 3 };
LinkedList<Integer> head1 = new LinkedList<>(Arrays.asList(arr1));
LinkedList<Integer> head2 = new LinkedList<>(Arrays.asList(arr2));
int x = 10 ;
System.out.println( "Count = " + countPairs(head1, head2, x));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def countPairs(head1, head2, x):
count = 0
while head1 is not None and head2 is not None :
if (head1.data + head2.data) = = x:
head1 = head1. next
head2 = head2. next
count + = 1
elif (head1.data + head2.data) > x:
head2 = head2. next
else :
head1 = head1. next
return count
if __name__ = = '__main__' :
head1 = None
head2 = None
head1 = push(head1, 7 )
head1 = push(head1, 5 )
head1 = push(head1, 3 )
head1 = push(head1, 1 )
head2 = push(head2, 2 )
head2 = push(head2, 3 )
head2 = push(head2, 5 )
head2 = push(head2, 8 )
x = 10
print ( "Count =" , countPairs(head1, head2, x))
|
Javascript
class Node{
constructor(data){
this .data = data;
this .next = null ;
}
}
function push(head_ref, new_data){
let new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function countPairs(head1, head2, x){
let count = 0;
while (head1 != null && head2 != null ){
if (head1.data + head2.data == x){
head1 = head1.next;
head2 = head2.next;
count++;
}
else if (head1.data + head2.data > x)
head2 = head2.next;
else
head1 = head1.next;
}
return count;
}
let head1 = null ;
let head2 = null ;
head1 = push(head1, 7);
head1 = push(head1, 5);
head1 = push(head1, 3);
head1 = push(head1, 1);
head2 = push(head2, 2);
head2 = push(head2, 3);
head2 = push(head2, 5);
head2 = push(head2, 8);
let x = 10;
console.log( "Count = " + countPairs(head1, head2, x));
|
C#
using System;
class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
class Program
{
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int countPairs(Node head1, Node head2, int x)
{
int count = 0;
while (head1 != null && head2 != null )
{
if ((head1.data + head2.data) == x)
{
head1 = head1.next;
head2 = head2.next;
count++;
}
else if ((head1.data + head2.data) > x)
{
head2 = head2.next;
}
else
{
head1 = head1.next;
}
}
return count;
}
static void Main( string [] args)
{
Node head1 = null ;
Node head2 = null ;
head1 = push(head1, 7);
head1 = push(head1, 5);
head1 = push(head1, 3);
head1 = push(head1, 1);
head2 = push(head2, 2);
head2 = push(head2, 3);
head2 = push(head2, 5);
head2 = push(head2, 8);
int x = 10;
Console.WriteLine( "Count = " + countPairs(head1, head2, x));
}
}
|
Time Complexity: O(n1*logn1) + O(n2*logn2)
Auxiliary Space: O(1)
Sorting will change the order of nodes. If order is important, then copy of the linked lists can be created and used.
Method 3 (Hashing): Hash table is implemented using unordered_set in C++. We store all first linked list elements in hash table. For elements of second linked list, we subtract every element from x and check the result in hash table. If result is present, we increment the count.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int countPairs( struct Node* head1, struct Node* head2,
int x)
{
int count = 0;
unordered_set< int > us;
while (head1 != NULL)
{
us.insert(head1->data);
head1 = head1->next;
}
while (head2 != NULL)
{
if (us.find(x - head2->data) != us.end())
count++;
head2 = head2->next;
}
return count;
}
int main()
{
struct Node* head1 = NULL;
struct Node* head2 = NULL;
push(&head1, 7);
push(&head1, 5);
push(&head1, 1);
push(&head1, 3);
push(&head2, 3);
push(&head2, 5);
push(&head2, 2);
push(&head2, 8);
int x = 10;
cout << "Count = "
<< countPairs(head1, head2, x);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
class GFG
{
static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2, int x)
{
int count = 0 ;
HashSet<Integer> us = new HashSet<Integer>();
Iterator<Integer> itr1 = head1.iterator();
while (itr1.hasNext())
{
us.add(itr1.next());
}
Iterator<Integer> itr2 = head2.iterator();
while (itr2.hasNext())
{
if (!(us.add(x - itr2.next())))
count++;
}
return count;
}
public static void main(String[] args)
{
Integer arr1[] = { 3 , 1 , 5 , 7 };
Integer arr2[] = { 8 , 2 , 5 , 3 };
LinkedList<Integer> head1 = new LinkedList<>(Arrays.asList(arr1));
LinkedList<Integer> head2 = new LinkedList<>(Arrays.asList(arr2));
int x = 10 ;
System.out.println( "Count = " + countPairs(head1, head2, x));
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def push(head_ref, new_data):
new_node = Node()
new_node.data = new_data;
new_node. next = (head_ref);
(head_ref) = new_node;
return head_ref
def countPairs(head1, head2, x):
count = 0 ;
us = set ()
while (head1 ! = None ):
us.add(head1.data);
head1 = head1. next ;
while (head2 ! = None ):
if ((x - head2.data) in us):
count + = 1
head2 = head2. next ;
return count;
if __name__ = = '__main__' :
head1 = None ;
head2 = None ;
head1 = push(head1, 7 );
head1 = push(head1, 5 );
head1 = push(head1, 1 );
head1 = push(head1, 3 );
head2 = push(head2, 3 );
head2 = push(head2, 5 );
head2 = push(head2, 2 );
head2 = push(head2, 8 );
x = 10 ;
print ( "Count =" , countPairs(head1, head2, x));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPairs(List< int > head1, List< int > head2, int x)
{
int count = 0;
HashSet< int > us = new HashSet< int >();
foreach ( int itr1 in head1)
{
us.Add(itr1);
}
foreach ( int itr2 in head2)
{
if (!(us.Contains(x - itr2)))
count++;
}
return count;
}
public static void Main(String[] args)
{
int []arr1 = {3, 1, 5, 7};
int []arr2 = {8, 2, 5, 3};
List< int > head1 = new List< int >(arr1);
List< int > head2 = new List< int >(arr2);
int x = 10;
Console.WriteLine( "Count = " + countPairs(head1, head2, x));
}
}
|
Javascript
<script>
class Node
{
constructor(new_data)
{
this .data = new_data;
this .next = null ;
}
};
function countPairs(head1, head2, x)
{
let count = 0;
let us = new Set();
while (head1 != null )
{
us.add(head1.data);
head1 = head1.next;
}
while (head2 != null )
{
if (us.has(x - head2.data))
count++;
head2 = head2.next;
}
return count;
}
let head1 = null ;
let head2 = null ;
head1 = new Node(3)
head1.next = new Node(1)
head1.next.next = new Node(5)
head1.next.next.next = new Node(7)
head2 = new Node(8)
head2.next = new Node(2)
head2.next.next = new Node(5)
head2.next.next.next = new Node(3)
let x = 10;
document.write( "Count = " +
countPairs(head1, head2, x));
</script>
|
Time Complexity: O(n1 + n2)
Auxiliary Space: O(n1), hash table should be created of the array having smaller size so as to reduce the space complexity.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.