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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
int weight;
Node* next;
Node( int d, int w)
: data(d), weight(w), next(NULL)
{
}
};
bool isTriangular( int n)
{
int x = sqrt (8 * n + 1);
return (x * x == 8 * n + 1);
}
int productOfTriangularNodes(Node* head)
{
int product = 1;
while (head) {
if (isTriangular(head->weight)) {
product *= head->data;
}
head = head->next;
}
return product;
}
int main()
{
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;
int product = productOfTriangularNodes(head);
cout << product << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
int data;
int weight;
Node next;
Node( int d, int w)
{
data = d;
weight = w;
next = null ;
}
}
static boolean isTriangular( int n)
{
int x = ( int )Math.sqrt( 8 * n + 1 );
return (x * x == 8 * n + 1 );
}
static int productOfTriangularNodes(Node head)
{
int product = 1 ;
while (head != null ) {
if (isTriangular(head.weight)) {
product *= head.data;
}
head = head.next;
}
return product;
}
public static void main(String[] args)
{
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 ;
int product = productOfTriangularNodes(head);
System.out.println(product);
}
}
|
Python3
import math
class Node:
def __init__( self , d, w):
self .data = d
self .weight = w
self . next = None
def isTriangular(n):
x = int (math.sqrt( 8 * n + 1 ))
return x * x = = 8 * n + 1
def productOfTriangularNodes(head):
product = 1
while head:
if isTriangular(head.weight):
product * = head.data
head = head. next
return product
if __name__ = = "__main__" :
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
product = productOfTriangularNodes(head)
print (product)
|
C#
using System;
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
{
static bool IsTriangular( int n)
{
int x = ( int )Math.Sqrt(8 * n + 1);
return (x * x == 8 * n + 1);
}
static int ProductOfTriangularNodes(Node head)
{
int product = 1;
while (head != null )
{
if (IsTriangular(head.weight))
{
product *= head.data;
}
head = head.next;
}
return product;
}
static void Main()
{
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 ;
int product = ProductOfTriangularNodes(head);
Console.WriteLine(product);
}
}
|
Javascript
class Node {
constructor(d, w) {
this .data = d;
this .weight = w;
this .next = null ;
}
}
function isTriangular(n) {
const x = Math.sqrt(8 * n + 1);
return Math.floor(x) === x;
}
function productOfTriangularNodes(head) {
let product = 1;
let current = head;
while (current !== null ) {
if (isTriangular(current.weight)) {
product *= current.data;
}
current = current.next;
}
return product;
}
( function () {
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 ;
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.
Share your thoughts in the comments
Please Login to comment...