Find product of nodes whose weights is catalan number in a Linked list
Last Updated :
04 Dec, 2023
Given a linked list with weights, the task is to find the product of nodes whose weights are Catalan numbers.
Examples:
Input: 3(1) -> 7 (4) -> 5 (2) -> 12 (5) -> 14 (3) -> NULL
Output: 180
Explanation: Catalan number weights are 1, 2, and 5, and the product of their nodes is 3 * 5 * 12 = 180.
Input: 45 (4) -> 21 (2) -> 5 (5) -> 42 (3) -> NULL
Output: 110
Explanation: Catalan number weights are 2 and 5, and the product of their nodes is 21 * 5 = 110.
Approach: To solve the problem follow the below Intuition:
The idea behind this approach is to traverse a linked list and calculate the product of nodes that have weights corresponding to Catalan numbers. Catalan numbers are a sequence of natural numbers that often appear in various counting problems, such as the number of different ways to arrange parentheses in a mathematical expression. In this code, the function isCatalan() is used to check if a given number is a Catalan number. The findProduct() function takes the head of the linked list as input and initializes the product to 1. It then iterates through each node in the linked list. If the weight of the current node is a Catalan number, it multiplies the product by the data value of that node. Finally, it returns the computed product.
Follow the steps to solve the problem:
- Define a function named isCatalan it checks whether a given number is a Catalan number. It uses an unordered set to store computed Catalan numbers. The function iteratively calculates Catalan numbers until the current Catalan number exceeds the given number. It checks if the given number is present in the set of computed Catalan numbers and returns true if it is, indicating that the number is a Catalan number.
- Define a function named findProduct it takes the head of a linked list as input and returns the product of nodes with Catalan number weights.
- Initialize a variable product to 1.
- Traverse the linked list starting from the head node. At each node, check if the weight of the node is a Catalan number by calling the isCatalan function. If it is, multiply the current product with the data value of that node.
- Move the current node to the next node in the linked list.
- Repeat steps 4-5 until the end of the linked list is reached (i.e., the current node becomes nullptr).
- Return the final value of product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
int weight;
Node* next;
Node( int value, int w)
{
data = value;
weight = w;
next = nullptr;
}
};
bool isCatalan( int num)
{
unordered_set< int > catalanNumbers;
int curr = 1;
int count = 1;
catalanNumbers.insert(curr);
while (curr <= num) {
curr = (curr * 2 * (2 * count - 1)) / (count + 1);
catalanNumbers.insert(curr);
count++;
}
return catalanNumbers.count(num);
}
long long findProduct(Node* head)
{
long long product = 1;
while (head != nullptr) {
if (isCatalan(head->weight)) {
product *= head->data;
}
head = head->next;
}
return product;
}
int main()
{
Node* head = new Node(3, 1);
Node* node2 = new Node(7, 4);
Node* node3 = new Node(5, 2);
Node* node4 = new Node(12, 5);
Node* node5 = new Node(14, 3);
head->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
long long product = findProduct(head);
cout << "Product of nodes with Catalan number weights: "
<< product << endl;
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
class Node {
int data;
int weight;
Node next;
Node( int value, int w) {
data = value;
weight = w;
next = null ;
}
}
public class Main {
static boolean isCatalan( int num) {
Set<Integer> catalanNumbers = new HashSet<>();
int curr = 1 ;
int count = 1 ;
catalanNumbers.add(curr);
while (curr <= num) {
curr = (curr * 2 * ( 2 * count - 1 )) / (count + 1 );
catalanNumbers.add(curr);
count++;
}
return catalanNumbers.contains(num);
}
static long findProduct(Node head) {
long product = 1 ;
while (head != null ) {
if (isCatalan(head.weight)) {
product *= head.data;
}
head = head.next;
}
return product;
}
public static void main(String[] args) {
Node head = new Node( 3 , 1 );
Node node2 = new Node( 7 , 4 );
Node node3 = new Node( 5 , 2 );
Node node4 = new Node( 12 , 5 );
Node node5 = new Node( 14 , 3 );
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
long product = findProduct(head);
System.out.println( "Product of nodes with Catalan number weights: " + product);
}
}
|
Python3
class Node:
def __init__( self , value, w):
self .data = value
self .weight = w
self . next = None
def is_catalan(num):
catalan_numbers = set ()
curr = 1
count = 1
catalan_numbers.add(curr)
while curr < = num:
curr = (curr * 2 * ( 2 * count - 1 )) / / (count + 1 )
catalan_numbers.add(curr)
count + = 1
return num in catalan_numbers
def find_product(head):
product = 1
current = head
while current is not None :
if is_catalan(current.weight):
product * = current.data
current = current. next
return product
if __name__ = = "__main__" :
head = Node( 3 , 1 )
node2 = Node( 7 , 4 )
node3 = Node( 5 , 2 )
node4 = Node( 12 , 5 )
node5 = Node( 14 , 3 )
head. next = node2
node2. next = node3
node3. next = node4
node4. next = node5
product = find_product(head)
print ( "Product of nodes with Catalan number weights:" , product)
|
C#
using System;
using System.Collections.Generic;
class Node
{
public int data;
public int weight;
public Node next;
public Node( int value, int w)
{
data = value;
weight = w;
next = null ;
}
}
class Program
{
static bool IsCatalan( int num)
{
HashSet< int > catalanNumbers = new HashSet< int >();
int curr = 1;
int count = 1;
catalanNumbers.Add(curr);
while (curr <= num)
{
curr = (curr * 2 * (2 * count - 1)) / (count + 1);
catalanNumbers.Add(curr);
count++;
}
return catalanNumbers.Contains(num);
}
static long FindProduct(Node head)
{
long product = 1;
while (head != null )
{
if (IsCatalan(head.weight))
{
product *= head.data;
}
head = head.next;
}
return product;
}
static void Main()
{
Node head = new Node(3, 1);
Node node2 = new Node(7, 4);
Node node3 = new Node(5, 2);
Node node4 = new Node(12, 5);
Node node5 = new Node(14, 3);
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
long product = FindProduct(head);
Console.WriteLine( "Product of nodes with Catalan number weights: " + product);
}
}
|
Javascript
class Node {
constructor(value, w) {
this .data = value;
this .weight = w;
this .next = null ;
}
}
function isCatalan(num) {
const catalanNumbers = new Set();
let curr = 1;
let count = 1;
catalanNumbers.add(curr);
while (curr <= num) {
curr = (curr * 2 * (2 * count - 1)) / (count + 1);
catalanNumbers.add(curr);
count++;
}
return catalanNumbers.has(num);
}
function findProduct(head) {
let product = 1;
while (head !== null ) {
if (isCatalan(head.weight)) {
product *= head.data;
}
head = head.next;
}
return product;
}
const head = new Node(3, 1);
const node2 = new Node(7, 4);
const node3 = new Node(5, 2);
const node4 = new Node(12, 5);
const node5 = new Node(14, 3);
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
const product = findProduct(head);
console.log( "Product of nodes with Catalan number weights: " + product);
|
Output
Product of nodes with Catalan number weights: 180
Time Complexity: O(n), where n is the number of nodes in the linked list, because we are iterating through each node once.
Auxiliary Space: O(log(num)), where num is the weight of the node with the maximum weight, because we are uses an unordered set to store computed Catalan numbers, which can grow up to log(num) elements.
Share your thoughts in the comments
Please Login to comment...