Given a linked list, and a product K. The task is to check if there exist two numbers in the linked list whose product is equal to the given number K. If there exist two numbers, print them. If there are multiple answers, print any of them.
Examples:
Input : List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL
K = 2
Output : Pair is (1, 2)
Input : List = 10 -> 12 -> 31 -> 42 -> 53 -> NULL
K = 15
Output : No Pair Exists
Method 1 (Brute force): Run two nested loops to generate all possible pairs of the linked list and check if the product of any pair matches with the given product K.
Below is the implementation of the above approach:
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 = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int check_pair_product( struct Node* head, int prod)
{
struct Node *p = head, *q;
while (p != NULL) {
q = p->next;
while (q != NULL) {
if ((p->data) * (q->data) == prod) {
cout << p->data << " " << q->data;
return true ;
}
q = q->next;
}
p = p->next;
}
return 0;
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
push(&head, 18);
push(&head, 47);
push(&head, 16);
push(&head, 12);
push(&head, 14);
bool res = check_pair_product(head, 26);
if (res == false )
cout << "NO PAIR EXIST" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
}
static Node head;
static void 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;
head = head_ref;
}
static boolean check_pair_product(Node head,
int prod)
{
Node p = head, q;
while (p != null )
{
q = p.next;
while (q != null )
{
if ((p.data) * (q.data) == prod)
{
System.out.print(p.data + " " +
q.data);
return true ;
}
q = q.next;
}
p = p.next;
}
return false ;
}
public static void main(String[] args)
{
head = null ;
push(head, 1 );
push(head, 4 );
push(head, 1 );
push(head, 12 );
push(head, 1 );
push(head, 18 );
push(head, 47 );
push(head, 16 );
push(head, 12 );
push(head, 14 );
boolean res = check_pair_product(head, 26 );
if (res == false )
System.out.println( "NO PAIR EXIST" );
}
}
|
Python3
class Node:
def __init__( self , data, next ):
self .data = data
self . next = next
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data, self .head)
self .head = new_node
def check_pair_product( self , prod):
p = self .head
while p ! = None :
q = p. next
while q ! = None :
if p.data * q.data = = prod:
print (p.data, q.data)
return True
q = q. next
p = p. next
return False
if __name__ = = "__main__" :
linkedlist = LinkedList()
linkedlist.push( 1 )
linkedlist.push( 4 )
linkedlist.push( 1 )
linkedlist.push( 12 )
linkedlist.push( 1 )
linkedlist.push( 18 )
linkedlist.push( 47 )
linkedlist.push( 16 )
linkedlist.push( 12 )
linkedlist.push( 14 )
res = linkedlist.check_pair_product( 26 )
if res = = False :
print ( "NO PAIR EXIST" )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
}
static Node head;
static void 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;
head = head_ref;
}
static Boolean check_pair_product(Node head,
int prod)
{
Node p = head, q;
while (p != null )
{
q = p.next;
while (q != null )
{
if ((p.data) * (q.data) == prod)
{
Console.Write(p.data + " " +
q.data);
return true ;
}
q = q.next;
}
p = p.next;
}
return false ;
}
public static void Main(String[] args)
{
head = null ;
push(head, 1);
push(head, 4);
push(head, 1);
push(head, 12);
push(head, 1);
push(head, 18);
push(head, 47);
push(head, 16);
push(head, 12);
push(head, 14);
Boolean res = check_pair_product(head, 26);
if (res == false )
Console.WriteLine( "NO PAIR EXIST" );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head;
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;
head = head_ref;
}
function check_pair_product(head , prod) {
var p = head, q;
while (p != null ) {
q = p.next;
while (q != null ) {
if ((p.data) * (q.data) == prod) {
document.write(p.data + " " + q.data);
return true ;
}
q = q.next;
}
p = p.next;
}
return false ;
}
head = null ;
push(head, 1);
push(head, 4);
push(head, 1);
push(head, 12);
push(head, 1);
push(head, 18);
push(head, 47);
push(head, 16);
push(head, 12);
push(head, 14);
var res = check_pair_product(head, 26);
if (res == false )
document.write( "NO PAIR EXIST" );
</script>
|
Time complexity: O(N2), where N is the length of the linked list.
Auxiliary Space: O(1)
Method 2 (using hashing):
- Take a hashtable.
- Now, start traversing the linked list and check if the given product is divisible by the current element of the linked list also check if (K/current_element) of the linked list is present in a hashtable or not.
- if yes, return “true” else insert the current element to the hashtable and make a traversing pointer point to the next element of linked list.
Below is the implementation of the above approach:
C++14
#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 = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
bool check_pair_product( struct Node* head, int prod)
{
unordered_set< int > s;
struct Node* p = head;
while (p != NULL) {
int curr = p->data;
if ((prod % curr == 0) && (s.find(prod / curr) != s.end())) {
cout << curr << " " << prod / curr;
return true ;
}
s.insert(p->data);
p = p->next;
}
return false ;
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 2);
push(&head, 1);
push(&head, 12);
push(&head, 1);
push(&head, 18);
push(&head, 47);
push(&head, 16);
push(&head, 12);
push(&head, 14);
bool res = check_pair_product(head, 24);
if (res == false )
cout << "NO PAIR EXIST" ;
return 0;
}
|
Java
import java.util.*;
class Solution {
static final int MAX = 100000 ;
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 boolean check_pair_product(Node head, int prod)
{
Vector<Integer> s = new Vector<Integer>();
Node p = head;
while (p != null ) {
int curr = p.data;
if ((prod % curr == 0 ) && (s.contains(prod / curr))) {
System.out.print(curr + " " + (prod / curr));
return true ;
}
s.add(p.data);
p = p.next;
}
return false ;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 1 );
head = push(head, 2 );
head = push(head, 1 );
head = push(head, 12 );
head = push(head, 1 );
head = push(head, 18 );
head = push(head, 47 );
head = push(head, 16 );
head = push(head, 12 );
head = push(head, 14 );
boolean res = check_pair_product(head, 24 );
if (res == false )
System.out.println( "NO PAIR EXIST" );
}
}
|
Python3
class Node:
def __init__( self , data, next ):
self .data = data
self . next = next
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data, self .head)
self .head = new_node
def check_pair_product( self , prod):
p = self .head
s = set ()
while p ! = None :
curr = p.data
if (prod % curr = = 0 and
(prod / / curr) in s):
print (curr, prod / / curr)
return True ;
s.add(p.data);
p = p. next ;
return False
if __name__ = = "__main__" :
linkedlist = LinkedList()
linkedlist.push( 1 )
linkedlist.push( 2 )
linkedlist.push( 1 )
linkedlist.push( 12 )
linkedlist.push( 1 )
linkedlist.push( 18 )
linkedlist.push( 47 )
linkedlist.push( 16 )
linkedlist.push( 12 )
linkedlist.push( 14 )
res = linkedlist.check_pair_product( 24 )
if res = = False :
print ( "NO PAIR EXIST" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static readonly int MAX = 100000;
public 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 bool check_pair_product(Node head, int prod)
{
List< int > s = new List< int >();
Node p = head;
while (p != null ) {
int curr = p.data;
if ((prod % curr == 0) && (s.Contains(prod / curr))) {
Console.Write(curr + " " + (prod / curr));
return true ;
}
s.Add(p.data);
p = p.next;
}
return false ;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 1);
head = push(head, 2);
head = push(head, 1);
head = push(head, 12);
head = push(head, 1);
head = push(head, 18);
head = push(head, 47);
head = push(head, 16);
head = push(head, 12);
head = push(head, 14);
bool res = check_pair_product(head, 24);
if (res == false )
Console.Write( "NO PAIR EXIST" );
}
}
|
Javascript
<script>
var MAX = 100000;
class Node {
constructor() {
this .data = 0;
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 check_pair_product(head, prod) {
var s = [];
var p = head;
while (p != null ) {
var curr = p.data;
if (prod % curr == 0 && s.includes(prod / curr)) {
document.write(curr + " " + prod / curr);
return true ;
}
s.push(p.data);
p = p.next;
}
return false ;
}
var head = null ;
head = push(head, 1);
head = push(head, 2);
head = push(head, 1);
head = push(head, 12);
head = push(head, 1);
head = push(head, 18);
head = push(head, 47);
head = push(head, 16);
head = push(head, 12);
head = push(head, 14);
var res = check_pair_product(head, 24);
if (res == false ) document.write( "NO PAIR EXIST" );
</script>
|
Complexity Analysis:
- Time complexity: O(N)
- Auxiliary complexity: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!