# Count pairs from two linked lists whose product is equal to a given value

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 = 10Output: 1 The pair is: (5, 2)Input: list1 = 4->3->5->7->11->2->1 list2 = 2->3->4->5->6->8-12 X = 9Output: 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++ program to count all pairs from both the ` `// linked lists whose product is equal to ` `// a given value ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `/* A Linked list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// function to insert a node at the ` `// beginning of the linked list ` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `/* allocate node */` ` ` `struct` `Node* new_node = (` `struct` `Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` ` ` `/* put in the data */` ` ` `new_node->data = new_data; ` ` ` ` ` `/* link the old list to the new node */` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `/* move the head to point to the new node */` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// Function to count all pairs from both the linked lists ` `// whose product is equal to a given value ` `int` `countPairs(` `struct` `Node* head1, ` `struct` `Node* head2, ` `int` `x) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `struct` `Node *p1, *p2; ` ` ` ` ` `// Traverse the 1st linked list ` ` ` `for` `(p1 = head1; p1 != NULL; p1 = p1->next) { ` ` ` `// for each node of 1st list ` ` ` `// Traverse the 2nd list ` ` ` `for` `(p2 = head2; p2 != NULL; p2 = p2->next) { ` ` ` `// if sum of pair is equal to 'x' ` ` ` `// increment count ` ` ` `if` `((p1->data * p2->data) == x) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// required count of pairs ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `struct` `Node* head1 = NULL; ` ` ` `struct` `Node* head2 = NULL; ` ` ` ` ` `// create linked list1 3->1->5->7 ` ` ` `push(&head1, 7); ` ` ` `push(&head1, 5); ` ` ` `push(&head1, 1); ` ` ` `push(&head1, 3); ` ` ` ` ` `// create linked list2 8->2->5->3 ` ` ` `push(&head2, 3); ` ` ` `push(&head2, 5); ` ` ` `push(&head2, 2); ` ` ` `push(&head2, 8); ` ` ` ` ` `int` `x = 10; ` ` ` ` ` `cout << ` `"Count = "` `<< countPairs(head1, head2, x); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Count = 1

**Time complexity :** O(N^2)

## Recommended Posts:

- Count pairs from two linked lists whose sum is equal to a given value
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- Find a triplet from three linked lists with sum equal to a given number
- Find pairs with given product in a sorted Doubly Linked List
- Count pairs in a binary tree whose sum is equal to a given value x
- Find count of common nodes in two Doubly Linked Lists
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Identical Linked Lists
- Check if two Linked Lists are permutations of each other
- Add two numbers represented by linked lists | Set 2
- Union and Intersection of two Linked Lists
- Merge Sort for Linked Lists
- Intersection of two Sorted Linked Lists

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.