Find product of nodes whose weights is triangular number in a Linked list
Last Updated :
13 Sep, 2023
Given a linked list with weights, the task is to find the product of nodes whose weights are all triangular numbers where a triangular number is a number that can be represented as the sum of consecutive positive integers starting from 1. In other words, a triangular number is the sum of the first n natural numbers, where n is a positive integer. The formula for the nth triangular number is:
Tn = 1 + 2 + 3 + ... + n = n(n+1)/2
For example, the first few triangular numbers are:
T1 = 1
T2 = 1 + 2 = 3
T3 = 1 + 2 + 3 = 6
T4 = 1 + 2 + 3 + 4 = 10
Examples:
Input: 2 (2) -> 5 (3) -> 4 (6) -> 7 (10) -> 2 (4) -> NULL
Output: 140
Explanation: Triangular number weights are 3, 6, and 10, and the product of their nodes is 5 * 4 * 7 = 140.
Input: 1 (1) -> 4 (10) -> 2 (3) -> 6 (21) -> 8 (36) -> NULL
Output: 384
Explanation: Triangular number weights are 1, 10, 3, 21, and 36, and the product of their nodes is 1 * 4 * 2 * 6 * 8 = 384.
Input: 3 (3) -> 1 (1) -> 4 (4) -> 5 (5) -> NULL
Output: 3
Explanation: Triangular number weights are 3, 1 and the product of their nodes is 3 * 1 = 3.
Approach: This can be solved with the following idea:
The approach is to solve this problem is to traverse the linked list, and for each node, check if its weight is a triangular number or not. If the weight is triangular, multiply the product variable with the data of that node. Finally, return the product. Then the function isTriangular() is used to check if a number is triangular or not. A number is triangular if and only if 8n+1 is a perfect square. This can be checked by calculating the square root of 8n+1 and checking if it is an integer. The function productOfTriangularNodes() takes the head of the linked list as input and initializes the product variable to 1. Then it traverses the linked list using a while loop, and for each node, checks if its weight is triangular using the isTriangular() function. If the weight is triangular, it multiplies the product variable with the data of that node. Finally, it returns the product.
Steps of the above approach:
- First, we define a function isTriangular() that takes an integer as input and returns a bool value indicating whether the integer is a triangular number or not.
- To check if a number is a triangular number, we calculate its square root and check if it is an integer.
- Then we define a function productOfTriangularNodes() that takes a linked list head pointer as input and returns an integer value indicating the product of all nodes whose weights are triangular numbers.
- Inside the productOfTriangularNodes() function, we initialize the product variable to 1 and traverse the linked list until the end.
- For each node, we check if its weight is a triangular number using the isTriangular() function. If it is, we multiply the product variable by the node's data value.
- Finally, we return the product variable, which contains the product of all nodes whose weights are triangular numbers.
Below is the implementation of the above approach:
C++
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Linked list node
struct Node {
int data;
int weight;
Node* next;
Node(int d, int w)
: data(d), weight(w), next(NULL)
{
}
};
// Function to check if a number is
// triangular
bool isTriangular(int n)
{
int x = sqrt(8 * n + 1);
return (x * x == 8 * n + 1);
}
// Function to find product of nodes
// with triangular weights
int productOfTriangularNodes(Node* head)
{
int product = 1;
while (head) {
if (isTriangular(head->weight)) {
product *= head->data;
}
head = head->next;
}
return product;
}
// Driver code
int main()
{
// Initialize linked list
Node* head = new Node(2, 2);
head->next = new Node(5, 3);
head->next->next = new Node(4, 6);
head->next->next->next = new Node(7, 10);
head->next->next->next->next = new Node(2, 4);
head->next->next->next->next->next = NULL;
// Function call
int product = productOfTriangularNodes(head);
cout << product << endl;
return 0;
}
Java
// JAVA code for the above approach:
import java.util.*;
class GFG {
// Linked list node
static class Node {
int data;
int weight;
Node next;
Node(int d, int w)
{
data = d;
weight = w;
next = null;
}
}
// Function to check if a number is triangular
static boolean isTriangular(int n)
{
int x = (int)Math.sqrt(8 * n + 1);
return (x * x == 8 * n + 1);
}
// Function to find product of nodes with triangular
// weights
static int productOfTriangularNodes(Node head)
{
int product = 1;
while (head != null) {
if (isTriangular(head.weight)) {
product *= head.data;
}
head = head.next;
}
return product;
}
// Driver code
public static void main(String[] args)
{
// Initialize linked list
Node head = new Node(2, 2);
head.next = new Node(5, 3);
head.next.next = new Node(4, 6);
head.next.next.next = new Node(7, 10);
head.next.next.next.next = new Node(2, 4);
head.next.next.next.next.next = null;
// Function call
int product = productOfTriangularNodes(head);
System.out.println(product);
}
}
// This code is contributed by rambabuguphka
Python3
import math
# Linked list node
class Node:
def __init__(self, d, w):
self.data = d
self.weight = w
self.next = None
# Function to check if a number is triangular
def isTriangular(n):
x = int(math.sqrt(8 * n + 1))
return x * x == 8 * n + 1
# Function to find product of nodes with triangular weights
def productOfTriangularNodes(head):
product = 1
while head:
if isTriangular(head.weight):
product *= head.data
head = head.next
return product
# Driver code
if __name__ == "__main__":
# Initialize linked list
head = Node(2, 2)
head.next = Node(5, 3)
head.next.next = Node(4, 6)
head.next.next.next = Node(7, 10)
head.next.next.next.next = Node(2, 4)
head.next.next.next.next.next = None
# Function call
product = productOfTriangularNodes(head)
print(product)
C#
// C# code for the above approach:
using System;
// Linked list node
class Node
{
public int data;
public int weight;
public Node next;
public Node(int d, int w)
{
data = d;
weight = w;
next = null;
}
}
class GFG
{
// Function to check if a number is
// triangular
static bool IsTriangular(int n)
{
int x = (int)Math.Sqrt(8 * n + 1);
return (x * x == 8 * n + 1);
}
// Function to find the product of nodes
// with triangular weights
static int ProductOfTriangularNodes(Node head)
{
int product = 1;
while (head != null)
{
if (IsTriangular(head.weight))
{
product *= head.data;
}
head = head.next;
}
return product;
}
// Driver code
static void Main()
{
// Initialize linked list
Node head = new Node(2, 2);
head.next = new Node(5, 3);
head.next.next = new Node(4, 6);
head.next.next.next = new Node(7, 10);
head.next.next.next.next = new Node(2, 4);
head.next.next.next.next.next = null;
// Function call
int product = ProductOfTriangularNodes(head);
Console.WriteLine(product);
}
}
JavaScript
// Linked list node
class Node {
constructor(d, w) {
this.data = d;
this.weight = w;
this.next = null;
}
}
// Function to check if a number is triangular
function isTriangular(n) {
const x = Math.sqrt(8 * n + 1);
return Math.floor(x) === x;
}
// Function to find product of nodes with triangular weights
function productOfTriangularNodes(head) {
let product = 1;
let current = head;
while (current !== null) {
if (isTriangular(current.weight)) {
product *= current.data;
}
current = current.next;
}
return product;
}
// Driver code
(function () {
// Initialize linked list
const head = new Node(2, 2);
head.next = new Node(5, 3);
head.next.next = new Node(4, 6);
head.next.next.next = new Node(7, 10);
head.next.next.next.next = new Node(2, 4);
head.next.next.next.next.next = null;
// Function call
const product = productOfTriangularNodes(head);
console.log(product);
})();
Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(1), because we are not using any additional data structure.
Similar Reads
Find product of nodes whose weights is catalan number in a Linked list
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) -> NULLOutput: 180Explanation: Catalan number weights are 1, 2, and 5, and the product of their nodes is 3 * 5 * 12
11 min read
Product of nodes with Bell Number weights in a Singly Linked List
Given a singly linked list containing nodes with numeric values and their corresponding weights. The goal is to find the product of the nodes whose weight corresponds Bell Number. Bell numbers are a sequence of numbers that count different combinatorial arrangements, particularly the partitions of a
13 min read
Find product of nodes with min and max weights in a singly linked list
Given a singly linked list with weights, the task is to find the product of nodes with minimum and maximum weights in the list. Examples: Input: 3 (4) -> 1 (2) -> 8 (14) -> 12 (1) -> 5 (9) -> 7 (3) -> NULLOutput: 96Explanation: Node with minimum weight is 12 and maximum weight is 8
7 min read
Replace every node in a linked list with its closest triangular number
Given a singly linked list, the task is to replace every node with its closest triangular number. Examples: Input: 3 -> 9 -> 21 -> 25 -> NULLOutput: 3 -> 10 -> 21 -> 28 -> NULLExplanation: The closest Triangular numbers for each node are: Node 1 (value = 3): Closest Triangula
8 min read
Product of the nodes of a Singly Linked List
Given a singly linked list, the task is to find the product of all of the nodes of the given linked list.Note: For empty/ null lists, return -1.Examples: Input : List = 7->6->8->4->1Output : 1344Explanation: Product of nodes: 7 * 6 * 8 * 4 * 1 = 1344Input : List = 1->7->3->9-
7 min read
Sum and Product of all Fibonacci Nodes of a Singly Linked List
Given a singly linked list containing N nodes, the task is to find the sum and product of all the nodes from the list whose data value is a Fibonacci number. Examples: Input: LL = 15 -> 16 -> 8 -> 6 -> 13 Output: Sum = 21, Product = 104 Explanation: The list contains 2 fibonacci data val
13 min read
Sum and Product of all even digit sum Nodes of a Singly Linked List
Given a singly linked list containing N nodes, the task is to find the sum and product of all the nodes from the list whose data value has an even digit sum. Examples: Input: 15 -> 16 -> 8 -> 6 -> 13 Output: Sum = 42 Product = 9360 Explanation: The sum of all digit of number in linked li
15+ min read
Sum and Product of all the nodes which are less than K in the linked list
Given a Linked List and a key K. The task is to calculate the sum and product all the nodes from the list that are lesser than the key K.Examples: Input: 12 -> 15 -> 9 -> 11 -> 5 -> 6, K = 9 Output: Sum = 11, Product = 30Input: 13 -> 4 -> 16 -> 9 -> 22 -> 45 -> 5 -
11 min read
Count nodes in the given tree whose weight is a fibonacci number
Given a tree with the weights of all the nodes, the task is to count the number of nodes whose weight is a Fibonacci number.Examples: Input: Output: 2 Explanation: Nodes having weights 5 and 8 are fibonacci nodes. Input: Output: 3 Explanation: Nodes having weights 1, 3 and 8 are fibonacci nodes. App
8 min read
Count the nodes in the given Tree whose weight is a Perfect Number
Given a tree, and the weights of all the nodes, the task is to count the number of nodes whose weight is a Perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors. Examples: Input: Output: 0 Explanation: There is no node with a weight that is a perfect
8 min read