Count pairs from two linked lists whose product is equal to a given value
Last Updated :
06 Feb, 2023
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 product is equal to the given value x.
Note:The pair must have an element from each linked list.
Examples:
Input : list1 = 3->1->5->7
list2 = 8->2->5->3
X = 10
Output : 1
The pair is: (5, 2)
Input : list1 = 4->3->5->7->11->2->1
list2 = 2->3->4->5->6->8-12
X = 9
Output : 1
The pair is: (3, 3)
A simple approach is using two loops pick elements from both the linked lists and check whether the product of the pair is equal to the given value X or not. Count all such pairs and print the result.
Below is the implementation of the above approach:
C++
#include <iostream>
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
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = 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 ;
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;
}
public static void main(String[] args)
{
Node head1 = null ;
Node head2 = null ;
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 );
int x = 10 ;
System.out.print( "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.data = 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 = " + str (countPairs(head1, head2, x)))
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = 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;
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;
}
public static void Main(String[] args)
{
Node head1 = null ;
Node head2 = null ;
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);
int x = 10;
Console.Write( "Count = " + countPairs(head1, head2, x));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function countPairs(head1, head2 , x) {
var count = 0;
var 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;
}
var head1 = null ;
var head2 = null ;
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);
var x = 10;
document.write( "Count = " + countPairs(head1, head2, x));
</script>
|
Time complexity: O(N^2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...