Mean of distinct odd fibonacci nodes in a Linked List
Last Updated :
23 Jul, 2025
Given a singly linked list containing N nodes, the task is to find the mean of all the distinct nodes from the list whose data value is an odd Fibonacci number.
Examples:
Input: LL = 5 -> 21 -> 8 ->12-> 3 -> 13 ->144 -> 6
Output 10.5
Explanation:
Fibonacci Nodes present in the Linked List are {5, 21, 8, 3, 13, 144}
Odd Fibonacci Nodes present in the List are {5, 21, 3, 13}
Count of Odd Fibonacci Nodes is 4
Therefore , Mean of Odd Fibonacci Node Values = (5 + 21 + 3 + 13) / 4 = 10.5
Input: LL = 55 -> 3 -> 91 -> 89 -> 76 -> 233 -> 34 -> 87 -> 5 -> 100
Output:77
Explanation:
Fibonacci Nodes present in the Linked List are {55, 3, 89, 233, 34, 5}
Odd Fibonacci Nodes present in the Linked List are {55, 3, 89, 233, 5}
Count of Odd Fibonacci Nodes is 5
Therefore , Mean of Odd Fibonacci Node Values = (55 + 5 + 3 + 89 + 233) / 5 = 77
Approach:The idea is to use hashing to pre-compute and store all Fibonacci numbers up to the largest element in the linked list.
Follow the steps given below to solve the problem:
- Initialize two variables, say cnt, sum to store the count of odd Fibonacci nodes and the sum of all odd Fibonacci nodes respectively.
- Traverse the singly linked list and store the largest element of the linked list, say Max.
- Create a set, say hashmap to store all the Fibonacci numbers up to Max.
- Traverse the linked list and check if the current node is an odd and Fibonacci number or not. If found to be true, then increment the value of cnt and add the data value of the current node to sum and remove the node from Hashmap.
- Finally, print the value of (sum / cnt) as the required answer.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Structure of a
// singly Linked List
struct Node {
// Stores data value
// of a Node
int data;
// Stores pointer
// to next Node
Node* next;
};
// Function to insert a node at the
// beginning of the singly Linked List
void push(Node** head_ref, int new_data)
{
// Create a new Node
Node* new_node = new Node;
// Insert the data into
// the Node
new_node->data = new_data;
// Insert pointer to
// the next Node
new_node->next = (*head_ref);
// Update head_ref
(*head_ref) = new_node;
}
// Function to find the largest
// element from the linked list
int largestElement(struct Node* head_ref)
{
// Stores the largest element
// in the linked list
int Max = INT_MIN;
Node* head = head_ref;
// Iterate over the linked list
while (head != NULL) {
// If max is less than
// head->data
if (Max < head->data) {
// Update max
Max = head->data;
}
// Update head
head = head->next;
}
return Max;
}
// Function to store all Fibonacci numbers
// up to the largest element of the list
set<int> createHashMap(int Max)
{
// Store all Fibonacci numbers
// up to Max
set<int> hashmap;
// Stores first element of
// Fibonacci number
int prev = 0;
// Stores second element of
// Fibonacci number
int curr = 1;
// Insert prev into hashmap
hashmap.insert(prev);
// Insert curr into hashmap
hashmap.insert(curr);
// Insert all elements of
// Fibonacci numbers up to Max
while (curr <= Max) {
// Stores current fibonacci number
int temp = curr + prev;
// Insert temp into hashmap
hashmap.insert(temp);
// Update prev
prev = curr;
// Update curr
curr = temp;
}
return hashmap;
}
// Function to find the mean
// of odd Fibonacci nodes
double meanofnodes(struct Node* head)
{
// Stores the largest element
// in the linked list
int Max = largestElement(head);
// Stores all fibonacci numbers
// up to Max
set<int> hashmap
= createHashMap(Max);
// Stores current node
// of linked list
Node* curr = head;
// Stores count of
// odd Fibonacci nodes
int cnt = 0;
// Stores sum of all
// odd fibonacci nodes
double sum = 0.0;
// Traverse the linked list
while (curr != NULL) {
// if the data value of
// current node is an odd number
if ((curr->data) & 1){
// if data value of the node
// is present in hashmap
if (hashmap.count(curr->data)) {
// Update cnt
cnt++;
// Update sum
sum += curr->data;
// Remove current fibonacci number
// from hashmap so that duplicate
// elements can't be counted
hashmap.erase(curr->data);
}
}
// Update curr
curr = curr->next;
}
// Return the required mean
return (sum / cnt);
}
// Driver Code
int main()
{
// Stores head node of
// the linked list
Node* head = NULL;
// Insert all data values
// in the linked list
push(&head, 5);
push(&head, 21);
push(&head, 8);
push(&head, 12);
push(&head, 3);
push(&head, 13);
push(&head, 144);
push(&head, 6);
cout<<meanofnodes(head);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Structure of a
// singly Linked List
static class Node
{
// Stores data value
// of a Node
int data;
// Stores pointer
// to next Node
Node next;
};
static Node head;
// Function to insert a
// node at the beginning
// of the singly Linked List
static Node push(Node head_ref,
int new_data)
{
// Create a new Node
Node new_node = new Node();
// Insert the data into
// the Node
new_node.data = new_data;
// Insert pointer to
// the next Node
new_node.next = head_ref;
// Update head_ref
head_ref = new_node;
return head_ref;
}
// Function to find the largest
// element from the linked list
static int largestElement(Node head_ref)
{
// Stores the largest element
// in the linked list
int Max = Integer.MIN_VALUE;
Node head = head_ref;
// Iterate over the
// linked list
while (head != null)
{
// If max is less than
// head.data
if (Max < head.data)
{
// Update max
Max = head.data;
}
// Update head
head = head.next;
}
return Max;
}
// Function to store all
// Fibonacci numbers up
// to the largest element
// of the list
static HashSet<Integer>
createHashMap(int Max)
{
// Store all Fibonacci
// numbers up to Max
HashSet<Integer> hashmap =
new HashSet<>();
// Stores first element of
// Fibonacci number
int prev = 0;
// Stores second element of
// Fibonacci number
int curr = 1;
// Insert prev into hashmap
hashmap.add(prev);
// Insert curr into hashmap
hashmap.add(curr);
// Insert all elements of
// Fibonacci numbers up
// to Max
while (curr <= Max)
{
// Stores current fibonacci
// number
int temp = curr + prev;
// Insert temp into hashmap
hashmap.add(temp);
// Update prev
prev = curr;
// Update curr
curr = temp;
}
return hashmap;
}
// Function to find the mean
// of odd Fibonacci nodes
static double meanofnodes()
{
// Stores the largest element
// in the linked list
int Max = largestElement(head);
// Stores all fibonacci numbers
// up to Max
HashSet<Integer> hashmap =
createHashMap(Max);
// Stores current node
// of linked list
Node curr = head;
// Stores count of
// odd Fibonacci nodes
int cnt = 0;
// Stores sum of all
// odd fibonacci nodes
double sum = 0.0;
// Traverse the linked list
while (curr != null)
{
// if the data value of
// current node is an
// odd number
if ((curr.data) %2== 1)
{
// if data value of the node
// is present in hashmap
if (hashmap.contains(curr.data))
{
// Update cnt
cnt++;
// Update sum
sum += curr.data;
// Remove current fibonacci
// number from hashmap so that
// duplicate elements can't be
// counted
hashmap.remove(curr.data);
}
}
// Update curr
curr = curr.next;
}
// Return the required mean
return (sum / cnt);
}
// Driver Code
public static void main(String[] args)
{
// Stores head node of
// the linked list
head = null;
// Insert all data values
// in the linked list
head = push(head, 5);
head = push(head, 21);
head = push(head, 8);
head = push(head, 12);
head = push(head, 3);
head = push(head, 13);
head = push(head, 144);
head = push(head, 6);
System.out.print(meanofnodes());
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Structure of a
# singly Linked List
class Node:
def __init__(self):
# Stores data value
# of a Node
self.data = 0
# Stores pointer
# to next Node
self.next = None
# Function to add a node at the
# beginning of the singly Linked List
def push( head_ref, new_data):
# Create a new Node
new_node = Node()
# Insert the data into
# the Node
new_node.data = new_data;
# Insert pointer to
# the next Node
new_node.next = head_ref
# Update head_ref
head_ref = new_node;
return head_ref
# Function to find the largest
# element from the linked list
def largestElement(head_ref):
# Stores the largest element
# in the linked list
Max = -10000000
head = head_ref;
# Iterate over the linked list
while (head != None):
# If max is less than
# head.data
if (Max < head.data):
# Update max
Max = head.data;
# Update head
head = head.next;
return Max;
# Function to store all Fibonacci numbers
# up to the largest element of the list
def createHashMap(Max):
# Store all Fibonacci numbers
# up to Max
hashmap = set()
# Stores first element of
# Fibonacci number
prev = 0;
# Stores second element of
# Fibonacci number
curr = 1;
# Insert prev into hashmap
hashmap.add(prev);
# Insert curr into hashmap
hashmap.add(curr);
# Insert all elements of
# Fibonacci numbers up to Max
while (curr <= Max):
# Stores current fibonacci number
temp = curr + prev;
# Insert temp into hashmap
hashmap.add(temp);
# Update prev
prev = curr;
# Update curr
curr = temp;
return hashmap;
# Function to find the mean
# of odd Fibonacci nodes
def meanofnodes(head):
# Stores the largest element
# in the linked list
Max = largestElement(head);
# Stores all fibonacci numbers
# up to Max
hashmap = createHashMap(Max);
# Stores current node
# of linked list
curr = head;
# Stores count of
# odd Fibonacci nodes
cnt = 0;
# Stores sum of all
# odd fibonacci nodes
sum = 0.0;
# Traverse the linked list
while (curr != None):
# if the data value of
# current node is an odd number
if ((curr.data) % 2 == 1):
# if data value of the node
# is present in hashmap
if (curr.data in hashmap):
# Update cnt
cnt += 1
# Update sum
sum += curr.data;
# Remove current fibonacci number
# from hashmap so that duplicate
# elements can't be counted
hashmap.remove(curr.data);
# Update curr
curr = curr.next;
# Return the required mean
return (sum / cnt);
# Driver Code
if __name__=='__main__':
# Stores head node of
# the linked list
head = None;
# Insert all data values
# in the linked list
head = push(head, 5);
head = push(head, 21);
head = push(head, 8);
head = push(head, 12);
head = push(head, 3);
head = push(head, 13);
head = push(head, 144);
head = push(head, 6);
print(meanofnodes(head))
# This code is contributed by rutvik_56
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Structure of a
// singly Linked List
public class Node
{
// Stores data value
// of a Node
public int data;
// Stores pointer
// to next Node
public Node next;
};
static Node head;
// Function to insert a
// node at the beginning
// of the singly Linked List
static Node push(Node head_ref,
int new_data)
{
// Create a new Node
Node new_node = new Node();
// Insert the data into
// the Node
new_node.data = new_data;
// Insert pointer to
// the next Node
new_node.next = head_ref;
// Update head_ref
head_ref = new_node;
return head_ref;
}
// Function to find the largest
// element from the linked list
static int largestElement(Node head_ref)
{
// Stores the largest element
// in the linked list
int Max = int.MinValue;
Node head = head_ref;
// Iterate over the
// linked list
while (head != null)
{
// If max is less than
// head.data
if (Max < head.data)
{
// Update max
Max = head.data;
}
// Update head
head = head.next;
}
return Max;
}
// Function to store all
// Fibonacci numbers up
// to the largest element
// of the list
static HashSet<int> createDictionary(int Max)
{
// Store all Fibonacci
// numbers up to Max
HashSet<int> hashmap = new HashSet<int>();
// Stores first element of
// Fibonacci number
int prev = 0;
// Stores second element of
// Fibonacci number
int curr = 1;
// Insert prev into hashmap
hashmap.Add(prev);
// Insert curr into hashmap
hashmap.Add(curr);
// Insert all elements of
// Fibonacci numbers up
// to Max
while (curr <= Max)
{
// Stores current fibonacci
// number
int temp = curr + prev;
// Insert temp into hashmap
hashmap.Add(temp);
// Update prev
prev = curr;
// Update curr
curr = temp;
}
return hashmap;
}
// Function to find the mean
// of odd Fibonacci nodes
static double meanofnodes()
{
// Stores the largest element
// in the linked list
int Max = largestElement(head);
// Stores all fibonacci numbers
// up to Max
HashSet<int> hashmap = createDictionary(Max);
// Stores current node
// of linked list
Node curr = head;
// Stores count of
// odd Fibonacci nodes
int cnt = 0;
// Stores sum of all
// odd fibonacci nodes
double sum = 0.0;
// Traverse the linked list
while (curr != null)
{
// if the data value of
// current node is an
// odd number
if ((curr.data) % 2 == 1)
{
// if data value of the node
// is present in hashmap
if (hashmap.Contains(curr.data))
{
// Update cnt
cnt++;
// Update sum
sum += curr.data;
// Remove current fibonacci
// number from hashmap so that
// duplicate elements can't be
// counted
hashmap.Remove(curr.data);
}
}
// Update curr
curr = curr.next;
}
// Return the required mean
return (sum / cnt);
}
// Driver Code
public static void Main(String[] args)
{
// Stores head node of
// the linked list
head = null;
// Insert all data values
// in the linked list
head = push(head, 5);
head = push(head, 21);
head = push(head, 8);
head = push(head, 12);
head = push(head, 3);
head = push(head, 13);
head = push(head, 144);
head = push(head, 6);
Console.Write(meanofnodes());
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// Javascript program to implement
// the above approach
// Structure of a
// singly Linked List
class Node {
constructor()
{
// Stores data value
// of a Node
this.data = 0;
// Stores pointer
// to next Node
this.next = null;
}
};
// Function to insert a node at the
// beginning of the singly Linked List
function push(head_ref, new_data)
{
// Create a new Node
var new_node = new Node();
// Insert the data into
// the Node
new_node.data = new_data;
// Insert pointer to
// the next Node
new_node.next = (head_ref);
// Update head_ref
(head_ref) = new_node;
return head_ref;
}
// Function to find the largest
// element from the linked list
function largestElement(head_ref)
{
// Stores the largest element
// in the linked list
var Max = -1000000000;
var head = head_ref;
// Iterate over the linked list
while (head != null) {
// If max is less than
// head.data
if (Max < head.data) {
// Update max
Max = head.data;
}
// Update head
head = head.next;
}
return Max;
}
// Function to store all Fibonacci numbers
// up to the largest element of the list
function createHashMap(Max)
{
// Store all Fibonacci numbers
// up to Max
var hashmap = new Set();
// Stores first element of
// Fibonacci number
var prev = 0;
// Stores second element of
// Fibonacci number
var curr = 1;
// Insert prev into hashmap
hashmap.add(prev);
// Insert curr into hashmap
hashmap.add(curr);
// Insert all elements of
// Fibonacci numbers up to Max
while (curr <= Max) {
// Stores current fibonacci number
var temp = curr + prev;
// Insert temp into hashmap
hashmap.add(temp);
// Update prev
prev = curr;
// Update curr
curr = temp;
}
return hashmap;
}
// Function to find the mean
// of odd Fibonacci nodes
function meanofnodes(head)
{
// Stores the largest element
// in the linked list
var Max = largestElement(head);
// Stores all fibonacci numbers
// up to Max
var hashmap
= createHashMap(Max);
// Stores current node
// of linked list
var curr = head;
// Stores count of
// odd Fibonacci nodes
var cnt = 0;
// Stores sum of all
// odd fibonacci nodes
var sum = 0.0;
// Traverse the linked list
while (curr != null) {
// if the data value of
// current node is an odd number
if ((curr.data) & 1){
// if data value of the node
// is present in hashmap
if (hashmap.has(curr.data)) {
// Update cnt
cnt++;
// Update sum
sum += curr.data;
// Remove current fibonacci number
// from hashmap so that duplicate
// elements can't be counted
hashmap.delete(curr.data);
}
}
// Update curr
curr = curr.next;
}
// Return the required mean
return (sum / cnt);
}
// Driver Code
// Stores head node of
// the linked list
var head = null;
// Insert all data values
// in the linked list
head = push(head, 5);
head = push(head, 21);
head = push(head, 8);
head = push(head, 12);
head = push(head, 3);
head = push(head, 13);
head = push(head, 144);
head = push(head, 6);
document.write( meanofnodes(head));
// This code is contributed by noob2000.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem