Skip to content
Related Articles

Related Articles

Improve Article

Count the nodes whose sum with X is a Fibonacci number

  • Last Updated : 24 Jun, 2021

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:
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.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
int ans = 0, x;
 
vector<int> graph[100];
vector<int> weight(100);
 
// Function that returns true if
// x is a perfect square
bool isPerfectSquare(long double x)
{
    // Find floating point value of
    // square root of x
    long double sr = sqrt(x);
 
    // If square root is an integer
    return ((sr - floor(sr)) == 0);
}
 
// Function that returns true
// if n is a fibonacci number
bool isFibonacci(int n)
{
    return isPerfectSquare(5 * n * n + 4)
           || isPerfectSquare(5 * n * n - 4);
}
 
// Function to perform dfs
void dfs(int node, int parent)
{
    // If weight of the current node
    // gives a fibonacci number
    // when x is added to it
    if (isFibonacci(weight[node] + x))
        ans += 1;
 
    for (int to : graph[node]) {
        if (to == parent)
            continue;
        dfs(to, node);
    }
}
 
// Driver code
int main()
{
    x = 5;
 
    // Weights of the node
    weight[1] = 4;
    weight[2] = 5;
    weight[3] = 3;
    weight[4] = 25;
    weight[5] = 16;
    weight[6] = 34;
 
    // Edges of the tree
    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




// Java implementation of the
// above approach
import java.util.*;
 
@SuppressWarnings("unchecked")
 
class GFG{
     
static int ans = 0, x;
 
static ArrayList []graph = new ArrayList[100];
static ArrayList weight = new ArrayList();
   
// Function that returns true if
// x is a perfect square
static boolean isPerfectSquare(double x)
{
   
  // Find floating point value of
  // square root of x
  double sr = Math.sqrt(x);
   
  // If square root is an integer
  return ((sr - Math.floor(sr)) == 0);
}
   
// Function that returns true
// if n is a fibonacci number
static boolean isFibonacci(int n)
{
  return isPerfectSquare(5 * n * n + 4) ||
         isPerfectSquare(5 * n * n - 4);
}
   
// Function to perform dfs
static void dfs(int node, int parent)
{
   
  // If weight of the current node
  // gives a fibonacci number
  // when x is added to it
  if (isFibonacci((int)weight.get(node) + x))
    ans += 1;
  
  for(int to : (ArrayList<Integer>)graph[node])
  {
    if (to == parent)
      continue;
     
    dfs(to, node);
  }
}
   
// Driver Code
public static void main(String[] args)
{
  x = 5;
   
  for(int i = 0; i < 100; i++)
  {
    weight.add(0);
    graph[i] = new ArrayList();
  }
   
  // Weights of the node
  weight.add(1, 4);
  weight.add(2, 5);
  weight.add(3, 3);
  weight.add(4, 25);
  weight.add(5, 16);
  weight.add(6, 34);
   
  // Edges of the tree
  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);
}
}
 
// This code is contributed by pratham76

C#




// C# implementation of the
// above approach
using System;
using System.Collections;
class GFG{
      
static int ans = 0, x;
static ArrayList []graph = new ArrayList[100];
static ArrayList weight = new ArrayList();
  
// Function that returns true if
// x is a perfect square
static bool isPerfectSquare(double x)
{
  // Find floating point value of
  // square root of x
  double sr = Math.Sqrt(x);
 
  // If square root is an integer
  return ((sr - Math.Floor(sr)) == 0);
}
  
// Function that returns true
// if n is a fibonacci number
static bool isFibonacci(int n)
{
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
  
// Function to perform dfs
static void dfs(int node, int parent)
{
  // If weight of the current node
  // gives a fibonacci number
  // when x is added to it
  if (isFibonacci((int)weight[node] + x))
    ans += 1;
 
  foreach (int to in graph[node])
  {
    if (to == parent)
      continue;
    dfs(to, node);
  }
}
  
// Driver Code
public static void Main(string[] args)
{
  x = 5;
 
  for(int i = 0; i < 100; i++)
  {
    weight.Add(0);
    graph[i] = new ArrayList();
  }
 
  // Weights of the node
  weight[1] = 4;
  weight[2] = 5;
  weight[3] = 3;
  weight[4] = 25;
  weight[5] = 16;
  weight[6] = 34;
 
  // Edges of the tree
  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);
}
}
 
// This code is contributed by rutvik_56

Javascript




<script>
 
// JavaScript implementation of the
// above approach
 
var ans = 0, x;
var graph = Array.from(Array(100), ()=>Array());
var weight = [];
  
// Function that returns true if
// x is a perfect square
function isPerfectSquare(x)
{
  // Find floating point value of
  // square root of x
  var sr = Math.sqrt(x);
 
  // If square root is an integer
  return ((sr - Math.floor(sr)) == 0);
}
  
// Function that returns true
// if n is a fibonacci number
function isFibonacci(n)
{
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
  
// Function to perform dfs
function dfs(node, parent)
{
  // If weight of the current node
  // gives a fibonacci number
  // when x is added to it
  if (isFibonacci(weight[node] + x))
    ans += 1;
 
  for(var to of graph[node])
  {
    if (to == parent)
      continue;
    dfs(to, node);
  }
}
  
// Driver Code
x = 5;
for(var i = 0; i < 100; i++)
{
  weight.push(0);
  graph[i] = [];
}
// Weights of the node
weight[1] = 4;
weight[2] = 5;
weight[3] = 3;
weight[4] = 25;
weight[5] = 16;
weight[6] = 34;
// Edges of the tree
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>
Output: 
2

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :