Two nodes in a Linked list whose product is equal to the target value
Last Updated :
12 Jul, 2023
Given a head of a singly linked list and a target value. The task is to find whether there exist any two nodes in the linked list whose product is equal to the target value.
Examples:
Input: Linked-List = 2->5->3->4->6, Target = 12
Output: True
Input: Linked-List = 1->4->3->7->2, Target = 5
Output: False
Approach: This can be solved with the following idea:
Using the Set data structure, check if the target is divided by a value in the linked list that is present or not. If present return true, otherwise return false.
Steps involved in the implementation of code:
- Declare a set seen.
- Iterate in a linked list:
- If target/ Curvalue is present in the set, return true.
- Otherwise, store the CurValue in the set.
- If no value is found after iterating return false.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node( int x)
{
data = x;
next = NULL;
}
};
bool hasTwoNodesWithProduct(Node* head, int target)
{
unordered_set< int > seen;
Node* curr = head;
while (curr != NULL) {
int currVal = curr->data;
int product = target / currVal;
if (target % currVal == 0
&& seen.find(product) != seen.end()) {
return true ;
}
seen.insert(currVal);
curr = curr->next;
}
return false ;
}
int main()
{
Node* head = new Node(2);
head->next = new Node(5);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(6);
int target = 12;
cout << hasTwoNodesWithProduct(head, target) << endl;
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node next;
Node( int x) {
data = x;
next = null ;
}
}
class Main {
static boolean hasTwoNodesWithProduct(Node head, int target) {
HashSet<Integer> seen = new HashSet<>();
Node curr = head;
while (curr != null ) {
int currVal = curr.data;
int product = target / currVal;
if (target % currVal == 0 && seen.contains(product)) {
return true ;
}
seen.add(currVal);
curr = curr.next;
}
return false ;
}
public static void main(String[] args) {
Node head = new Node( 2 );
head.next = new Node( 5 );
head.next.next = new Node( 3 );
head.next.next.next = new Node( 4 );
head.next.next.next.next = new Node( 6 );
int target = 12 ;
System.out.println(hasTwoNodesWithProduct(head, target));
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self . next = None
def hasTwoNodesWithProduct(head, target):
seen = set ()
curr = head
while curr:
currVal = curr.data
product = target / / currVal
if target % currVal = = 0 and product in seen:
return True
seen.add(currVal)
curr = curr. next
return False
if __name__ = = '__main__' :
head = Node( 2 )
head. next = Node( 5 )
head. next . next = Node( 3 )
head. next . next . next = Node( 4 )
head. next . next . next . next = Node( 6 )
target = 12
print (hasTwoNodesWithProduct(head, target))
|
C#
using System;
using System.Collections.Generic;
public class Node {
public int data;
public Node next;
public Node( int x)
{
data = x;
next = null ;
}
}
public class LinkedList {
public static bool HasTwoNodesWithProduct(Node head,
int target)
{
HashSet< int > seen = new HashSet< int >();
Node curr = head;
while (curr != null ) {
int currVal = curr.data;
int product = target / currVal;
if (target % currVal == 0
&& seen.Contains(product)) {
return true ;
}
seen.Add(currVal);
curr = curr.next;
}
return false ;
}
public static void Main( string [] args)
{
Node head = new Node(2);
head.next = new Node(5);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(6);
int target = 12;
Console.WriteLine(
HasTwoNodesWithProduct(head, target));
}
}
|
Javascript
class Node {
constructor(x) {
this .data = x;
this .next = null ;
}
}
function hasTwoNodesWithProduct(head, target) {
const seen = new Set();
let curr = head;
while (curr !== null ) {
const currVal = curr.data;
const product = Math.floor(target / currVal);
if (target % currVal === 0 && seen.has(product)) {
return true ;
}
seen.add(currVal);
curr = curr.next;
}
return false ;
}
function main() {
const head = new Node(2);
head.next = new Node(5);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(6);
const target = 12;
console.log(hasTwoNodesWithProduct(head, target));
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
Share your thoughts in the comments
Please Login to comment...