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++ 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 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 |
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# 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);
}
} |
// 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);
})(); |
140
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.