Count the nodes whose sum with X is a Fibonacci number
Last Updated :
17 Apr, 2023
Given a tree, and the weights of all the nodes and an integer X, the task is to count all the nodes i such that (weight[i] + X) is a Fibonacci Number.
First, few Fibonacci numbers are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, …
Examples:
Input:
X = 5
Output: 2
Only the nodes 3 and 5 give a fibonacci number when 5 is added to them.
i.e. (3 + 5) = 8 and (16 + 5) = 21 are both Fibonacci numbers.
Approach: Perform dfs on the tree and count all the nodes sum of whose weight with x is a Fibonacci number.
Algorithm:
- Create a static variable “ans” and “x” of integer type.
- Create an ArrayList of type ‘ArrayList’ called “graph” and an ArrayList of type ‘integer’ called “weight”.
- Create a method called “isPerfectSquare” that takes a double type argument x and checks if the square root of x is an integer or not.
- Create a method called “isFibonacci” that takes an integer type argument n and checks if 5nn+4 or 5nn-4 is a perfect square or not.
- Create a method called “dfs” that takes two integer type arguments node and parent.
- In the “dfs” method, if the sum of the weight of the current node and x is a Fibonacci number, increment the ans variable by 1.
- Traverse through the adjacent nodes of the current node and call the “dfs” method recursively for each adjacent node if it is not equal to the parent node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ans = 0, x;
vector< int > graph[100];
vector< int > weight(100);
bool isPerfectSquare( long double x)
{
long double sr = sqrt (x);
return ((sr - floor (sr)) == 0);
}
bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
void dfs( int node, int parent)
{
if (isFibonacci(weight[node] + x))
ans += 1;
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
}
}
int main()
{
x = 5;
weight[1] = 4;
weight[2] = 5;
weight[3] = 3;
weight[4] = 25;
weight[5] = 16;
weight[6] = 34;
graph[1].push_back(2);
graph[2].push_back(3);
graph[2].push_back(4);
graph[1].push_back(5);
graph[5].push_back(6);
dfs(1, 1);
cout << ans;
return 0;
}
|
Java
import java.util.*;
@SuppressWarnings ( "unchecked" )
class GFG{
static int ans = 0 , x;
static ArrayList []graph = new ArrayList[ 100 ];
static ArrayList weight = new ArrayList();
static boolean isPerfectSquare( double x)
{
double sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0 );
}
static boolean isFibonacci( int n)
{
return isPerfectSquare( 5 * n * n + 4 ) ||
isPerfectSquare( 5 * n * n - 4 );
}
static void dfs( int node, int parent)
{
if (isFibonacci(( int )weight.get(node) + x))
ans += 1 ;
for ( int to : (ArrayList<Integer>)graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
}
}
public static void main(String[] args)
{
x = 5 ;
for ( int i = 0 ; i < 100 ; i++)
{
weight.add( 0 );
graph[i] = new ArrayList();
}
weight.add( 1 , 4 );
weight.add( 2 , 5 );
weight.add( 3 , 3 );
weight.add( 4 , 25 );
weight.add( 5 , 16 );
weight.add( 6 , 34 );
graph[ 1 ].add( 2 );
graph[ 2 ].add( 3 );
graph[ 2 ].add( 4 );
graph[ 1 ].add( 5 );
graph[ 5 ].add( 6 );
dfs( 1 , 1 );
System.out.println(ans);
}
}
|
Python3
import math
ans, x = 0 , 0
graph = [[] for i in range ( 100 )]
weight = [ 0 ] * ( 100 )
def isPerfectSquare(x):
sr = math.sqrt(x);
return ((sr - math.floor(sr)) = = 0 )
def isFibonacci(n):
return isPerfectSquare( 5 * n * n + 4 ) or isPerfectSquare( 5 * n * n - 4 )
def dfs(node, parent):
global ans
if (isFibonacci(weight[node] + x)):
ans + = 1
for to in graph[node]:
if (to = = parent):
continue
dfs(to, node)
x = 5
weight[ 1 ] = 4
weight[ 2 ] = 5
weight[ 3 ] = 3
weight[ 4 ] = 25
weight[ 5 ] = 16
weight[ 6 ] = 34
graph[ 1 ].append( 2 )
graph[ 2 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 1 ].append( 5 )
graph[ 5 ].append( 6 )
dfs( 1 , 1 )
print (ans)
|
C#
using System;
using System.Collections;
class GFG{
static int ans = 0, x;
static ArrayList []graph = new ArrayList[100];
static ArrayList weight = new ArrayList();
static bool isPerfectSquare( double x)
{
double sr = Math.Sqrt(x);
return ((sr - Math.Floor(sr)) == 0);
}
static bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
static void dfs( int node, int parent)
{
if (isFibonacci(( int )weight[node] + x))
ans += 1;
foreach ( int to in graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
}
}
public static void Main( string [] args)
{
x = 5;
for ( int i = 0; i < 100; i++)
{
weight.Add(0);
graph[i] = new ArrayList();
}
weight[1] = 4;
weight[2] = 5;
weight[3] = 3;
weight[4] = 25;
weight[5] = 16;
weight[6] = 34;
graph[1].Add(2);
graph[2].Add(3);
graph[2].Add(4);
graph[1].Add(5);
graph[5].Add(6);
dfs(1, 1);
Console.Write(ans);
}
}
|
Javascript
<script>
var ans = 0, x;
var graph = Array.from(Array(100), ()=>Array());
var weight = [];
function isPerfectSquare(x)
{
var sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0);
}
function isFibonacci(n)
{
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
function dfs(node, parent)
{
if (isFibonacci(weight[node] + x))
ans += 1;
for ( var to of graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
}
}
x = 5;
for ( var i = 0; i < 100; i++)
{
weight.push(0);
graph[i] = [];
}
weight[1] = 4;
weight[2] = 5;
weight[3] = 3;
weight[4] = 25;
weight[5] = 16;
weight[6] = 34;
graph[1].push(2);
graph[2].push(3);
graph[2].push(4);
graph[1].push(5);
graph[5].push(6);
dfs(1, 1);
document.write(ans);
</script>
|
Time Complexity : O(n).
Auxilitary Space Complexity : O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...