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 values 8 and 13.
Therefore:
Sum = 8 + 13 = 21
Product = 8 * 13 = 104
Input: LL = 5 -> 3 -> 4 -> 2 -> 9
Output: Sum = 10, Product = 30
Explanation:
The list contains 3 fibonacci data values 5, 3 and 2.
Therefore:
Sum = 5 + 3 + 2 = 10
Product = 5 * 3 * 2 = 30
Approach: The idea is to use hashing to precompute and store the Fibonacci numbers, and then check if a node contains a Fibonacci value in O(1) time.
- Traverse through the entire linked list and obtain the maximum value in the list.
- Now, in order to check for the Fibonacci numbers, build a hash table containing all the Fibonacci numbers less than or equal to the maximum value in the linked list.
- Finally, traverse the nodes of the linked list one by one and check if the node contains Fibonacci numbers as their data value. Find the sum and product of the data of the nodes which are Fibonacci.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node
= (Node*) malloc (
sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int largestElement(
struct Node* head_ref)
{
int max = INT_MIN;
Node* head = head_ref;
while (head != NULL) {
if (max < head->data)
max = head->data;
head = head->next;
}
return max;
}
void createHash(set< int >& hash,
int maxElement)
{
int prev = 0, curr = 1;
hash.insert(prev);
hash.insert(curr);
while (curr <= maxElement) {
int temp = curr + prev;
hash.insert(temp);
prev = curr;
curr = temp;
}
}
void sumAndProduct(Node* head_ref)
{
int maxEle
= largestElement(head_ref);
set< int > hash;
createHash(hash, maxEle);
int prod = 1;
int sum = 0;
Node* ptr = head_ref;
while (ptr != NULL) {
if (hash.find(ptr->data)
!= hash.end()) {
prod *= ptr->data;
sum += ptr->data;
}
ptr = ptr->next;
}
cout << "Sum = " << sum << endl;
cout << "Product = " << prod;
}
int main()
{
Node* head = NULL;
push(&head, 13);
push(&head, 6);
push(&head, 8);
push(&head, 16);
push(&head, 15);
sumAndProduct(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int largestElement(
Node head_ref)
{
int max = Integer.MIN_VALUE;
Node head = head_ref;
while (head != null ) {
if (max < head.data)
max = head.data;
head = head.next;
}
return max;
}
static void createHash(HashSet<Integer> hash,
int maxElement)
{
int prev = 0 , curr = 1 ;
hash.add(prev);
hash.add(curr);
while (curr <= maxElement) {
int temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
static void sumAndProduct(Node head_ref)
{
int maxEle
= largestElement(head_ref);
HashSet<Integer> hash = new HashSet<Integer>();
createHash(hash, maxEle);
int prod = 1 ;
int sum = 0 ;
Node ptr = head_ref;
while (ptr != null ) {
if (hash.contains(ptr.data)) {
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
System.out.print( "Sum = " + sum + "\n" );
System.out.print( "Product = " + prod);
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 13 );
head = push(head, 6 );
head = push(head, 8 );
head = push(head, 16 );
head = push(head, 15 );
sumAndProduct(head);
}
}
|
Python3
import sys
class Node():
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def largestElement(head_ref):
max = - sys.maxsize
head = head_ref
while (head ! = None ):
if ( max < head.data):
max = head.data
head = head. next
return max
def createHash( hash , maxElement):
prev = 0
curr = 1
hash .add(prev)
hash .add(curr)
while (curr < = maxElement):
temp = curr + prev
hash .add(temp)
prev = curr
curr = temp
return hash
def sumAndProduct(head_ref):
maxEle = largestElement(head_ref)
hash = set ()
hash = createHash( hash , maxEle)
prod = 1
sum = 0
ptr = head_ref
while (ptr ! = None ):
if ptr.data in hash :
prod * = ptr.data
sum + = ptr.data
ptr = ptr. next
print ( "Sum =" , sum )
print ( "Product =" , prod)
if __name__ = = "__main__" :
head = None ;
head = push(head, 13 )
head = push(head, 6 )
head = push(head, 8 )
head = push(head, 16 )
head = push(head, 15 )
sumAndProduct(head)
|
C#
using System;
using System.Collections.Generic;
class GFG{
class Node {
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int largestElement(
Node head_ref)
{
int max = int .MinValue;
Node head = head_ref;
while (head != null ) {
if (max < head.data)
max = head.data;
head = head.next;
}
return max;
}
static void createHash(HashSet< int > hash,
int maxElement)
{
int prev = 0, curr = 1;
hash.Add(prev);
hash.Add(curr);
while (curr <= maxElement) {
int temp = curr + prev;
hash.Add(temp);
prev = curr;
curr = temp;
}
}
static void sumAndProduct(Node head_ref)
{
int maxEle
= largestElement(head_ref);
HashSet< int > hash = new HashSet< int >();
createHash(hash, maxEle);
int prod = 1;
int sum = 0;
Node ptr = head_ref;
while (ptr != null ) {
if (hash.Contains(ptr.data)) {
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
Console.Write( "Sum = " + sum + "\n" );
Console.Write( "Product = " + prod);
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 13);
head = push(head, 6);
head = push(head, 8);
head = push(head, 16);
head = push(head, 15);
sumAndProduct(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function largestElement(head_ref) {
var max = Number.MIN_VALUE;
var head = head_ref;
while (head != null ) {
if (max < head.data)
max = head.data;
head = head.next;
}
return max;
}
function createHash( hash , maxElement) {
var prev = 0, curr = 1;
hash.add(prev);
hash.add(curr);
while (curr <= maxElement) {
var temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
function sumAndProduct(head_ref) {
var maxEle = largestElement(head_ref);
var hash = new Set();
createHash(hash, maxEle);
var prod = 1;
var sum = 0;
var ptr = head_ref;
while (ptr != null ) {
if (hash.has(ptr.data)) {
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
document.write( "Sum = " + sum + "<br/>" );
document.write( "Product = " + prod);
}
var head = null ;
head = push(head, 13);
head = push(head, 6);
head = push(head, 8);
head = push(head, 16);
head = push(head, 15);
sumAndProduct(head);
</script>
|
Output:
Sum = 21
Product = 104
Time Complexity: O(N), where N is the number of nodes in the linked list.
Auxiliary Space: O(1) because it is using constant space
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Aug, 2022
Like Article
Save Article