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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void 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;
}
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;
}
set< int > createHashMap( int Max)
{
set< int > hashmap;
int prev = 0;
int curr = 1;
hashmap.insert(prev);
hashmap.insert(curr);
while (curr <= Max) {
int temp = curr + prev;
hashmap.insert(temp);
prev = curr;
curr = temp;
}
return hashmap;
}
double meanofnodes( struct Node* head)
{
int Max = largestElement(head);
set< int > hashmap
= createHashMap(Max);
Node* curr = head;
int cnt = 0;
double sum = 0.0;
while (curr != NULL) {
if ((curr->data) & 1){
if (hashmap.count(curr->data)) {
cnt++;
sum += curr->data;
hashmap.erase(curr->data);
}
}
curr = curr->next;
}
return (sum / cnt);
}
int main()
{
Node* head = NULL;
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
import java.util.*;
class GFG{
static class Node
{
int data;
Node next;
};
static Node head;
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 HashSet<Integer>
createHashMap( int Max)
{
HashSet<Integer> hashmap =
new HashSet<>();
int prev = 0 ;
int curr = 1 ;
hashmap.add(prev);
hashmap.add(curr);
while (curr <= Max)
{
int temp = curr + prev;
hashmap.add(temp);
prev = curr;
curr = temp;
}
return hashmap;
}
static double meanofnodes()
{
int Max = largestElement(head);
HashSet<Integer> hashmap =
createHashMap(Max);
Node curr = head;
int cnt = 0 ;
double sum = 0.0 ;
while (curr != null )
{
if ((curr.data) % 2 == 1 )
{
if (hashmap.contains(curr.data))
{
cnt++;
sum += curr.data;
hashmap.remove(curr.data);
}
}
curr = curr.next;
}
return (sum / cnt);
}
public static void main(String[] args)
{
head = null ;
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());
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def push( head_ref, new_data):
new_node = Node()
new_node.data = new_data;
new_node. next = head_ref
head_ref = new_node;
return head_ref
def largestElement(head_ref):
Max = - 10000000
head = head_ref;
while (head ! = None ):
if ( Max < head.data):
Max = head.data;
head = head. next ;
return Max ;
def createHashMap( Max ):
hashmap = set ()
prev = 0 ;
curr = 1 ;
hashmap.add(prev);
hashmap.add(curr);
while (curr < = Max ):
temp = curr + prev;
hashmap.add(temp);
prev = curr;
curr = temp;
return hashmap;
def meanofnodes(head):
Max = largestElement(head);
hashmap = createHashMap( Max );
curr = head;
cnt = 0 ;
sum = 0.0 ;
while (curr ! = None ):
if ((curr.data) % 2 = = 1 ):
if (curr.data in hashmap):
cnt + = 1
sum + = curr.data;
hashmap.remove(curr.data);
curr = curr. next ;
return ( sum / cnt);
if __name__ = = '__main__' :
head = None ;
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))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Node
{
public int data;
public Node next;
};
static Node head;
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 HashSet< int > createDictionary( int Max)
{
HashSet< int > hashmap = new HashSet< int >();
int prev = 0;
int curr = 1;
hashmap.Add(prev);
hashmap.Add(curr);
while (curr <= Max)
{
int temp = curr + prev;
hashmap.Add(temp);
prev = curr;
curr = temp;
}
return hashmap;
}
static double meanofnodes()
{
int Max = largestElement(head);
HashSet< int > hashmap = createDictionary(Max);
Node curr = head;
int cnt = 0;
double sum = 0.0;
while (curr != null )
{
if ((curr.data) % 2 == 1)
{
if (hashmap.Contains(curr.data))
{
cnt++;
sum += curr.data;
hashmap.Remove(curr.data);
}
}
curr = curr.next;
}
return (sum / cnt);
}
public static void Main(String[] args)
{
head = null ;
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());
}
}
|
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 = -1000000000;
var head = head_ref;
while (head != null ) {
if (Max < head.data) {
Max = head.data;
}
head = head.next;
}
return Max;
}
function createHashMap(Max)
{
var hashmap = new Set();
var prev = 0;
var curr = 1;
hashmap.add(prev);
hashmap.add(curr);
while (curr <= Max) {
var temp = curr + prev;
hashmap.add(temp);
prev = curr;
curr = temp;
}
return hashmap;
}
function meanofnodes(head)
{
var Max = largestElement(head);
var hashmap
= createHashMap(Max);
var curr = head;
var cnt = 0;
var sum = 0.0;
while (curr != null ) {
if ((curr.data) & 1){
if (hashmap.has(curr.data)) {
cnt++;
sum += curr.data;
hashmap. delete (curr.data);
}
}
curr = curr.next;
}
return (sum / cnt);
}
var head = null ;
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...