Mean of distinct odd fibonacci nodes in a Linked List
Last Updated :
18 Jun, 2021
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
Find sum of even and odd nodes in a linked list
Given a linked list, the task is to find the sum of even and odd nodes in it separately. Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 Output: Even Sum = 12 Odd Sum = 16 Input: 5 -> 7 -> 8 -> 10 -> 15 Output: Even Sum = 18 Odd Sum = 27 Approach: Traverse the whole li
6 min read
Sum of all distinct nodes in a linked list
Given a linked list and it may consist of duplicate nodes. The task is to find the sum of non-duplicate nodes. Examples: Input: 1 -> 2 -> 1 -> 3 -> 4 -> 3 -> NULL Output: 6 2 and 4 are the only non-duplicate nodes and 2 + 4 = 6. Input: 1 -> 3 -> 1 -> 3 -> 1 -> 3 -
7 min read
Find first node of loop in a linked list
Given the head of a linked list that may contain a loop. A loop means that the last node of the linked list is connected back to a node in the same list. The task is to find the Starting node of the loop in the linked list if there is no loop in the linked list return -1.Example: Input: Output: 3Exp
14 min read
Move the First Fibonacci Number to the End of a Linked List
Given a singly linked list, the task is to identify the first Fibonacci number in the list and move that node to the end of the linked list. Examples: Input: 10 -> 15 -> 8 -> 13 -> 21 -> 5 -> 2 -> NULLOutput: 10 -> 15 -> 13 -> 21 -> 5 -> 2 -> 8 -> NULLExplan
12 min read
Finding Median in a Sorted Linked List
Given A sorted linked list of N elements. The task is to find the median in the given Sorted Linked List.We know that median in a sorted array is the middle element. Procedure to find median of N sorted numbers: if N is odd: median is N/2th elementelse median is N/2th element + (N/2+1)th elementExam
14 min read
Find the Highest Occurring Digit in a Linked List Nodes
Given a linked list, the task is to find the highest occurring digit in the linked list. Each node in the linked list contains a positive integer value. Analyze the digits present in these values and determine the digit that occurs the most frequently throughout the linked list. Examples: Input: 2 -
11 min read
Find the fractional (or n/k - th) node in linked list
Given a singly linked list and a number k, write a function to find the (n/k)-th element, where n is the number of elements in the list. We need to consider ceil value in case of decimals.Examples: Input: 1->2->3->4->5->6 , k = 2Output: 3Explanation: 6/2th element is the 3rd(1-based i
10 min read
Find modular node in a linked list
Given a singly linked list and a number k, find the last node whose n%k == 0, where n is the number of nodes in the list. Examples: Input : list = 1->2->3->4->5->6->7 k = 3 Output : 6 Input : list = 3->7->1->9->8 k = 2 Output : 9Recommended PracticeModular NodeTry It!Ta
5 min read
Various operations on Fibonacci nodes in a Singly Linked list
Given a singly linked list containing N nodes, the task is to perform the following operations on the Fibonacci nodes present in it: Print all Fibonacci nodes present in the singly linked list.Find the total count of Fibonacci nodes present in the singly linked list.Find the minimum and the maximum
15+ min read
Delete all odd nodes of a Circular Linked List
Prerequisite: Delete all the even nodes of a Circular Linked ListGiven a circular singly linked list containing N nodes, the task is to delete all the odd nodes from the list. Examples: Input: 572->112->21->5->1->6 Output: 572 -> 112 -> 6 Explanation: All the odd valued nodes ha
10 min read