
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count the nodes whose weight is a perfect square in C++
Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the number is a perfect square. If weight is 36 then it is 62 so this node will be counted.
For Example
Input
The tree which will be created after inputting the values is given below −
Output
Count the nodes whose weight is a perfect square are: 4
Explanation
we are given with the tree nodes and the weights associated with each node. Now we check whether the digits of nodes are perfect squares or not.
Node | Weight | Perfect square | Yes/no |
---|---|---|---|
2 | 121 | 11*11 | yes |
1 | 81 | 9*9 | yes |
4 | 37 | Prime number | no |
3 | 25 | 5*5 | yes |
8 | 100 | 10*10 | yes |
9 | 701 | Not possible | no |
Input
The tree which will be created after inputting the values is given below −
Output
Count the nodes whose weight is a perfect square are: 2
Explanation
we are given with the tree nodes and the weights associated with each node. Now we check whether the digits of nodes are perfect squares or not.
Node | Weight | Perfect square | Yes / No |
---|---|---|---|
2 | 11 | Not possible | no |
1 | 16 | 4*4 | yes |
4 | 4 | 2*2 | yes |
3 | 26 | Not possible | no |
8 | 1001 | Not possible | no |
Approach used in the below program is as follows −
In this approach we will apply DFS on the graph of the tree to traverse it and check if the weight of the node is a perfect square. Take two vectors Node_Weight(100) and edge_graph[100] for this purpose.
Initialize Node_Weight[] with the weights of nodes.
Create a tree using vector edge_graph.
Take a global variable square and initialize it with 0.
Function check(int check_it) takes an integer and returns true if check_it is a perfect square.
Take total = sqrt(check_it)
Now if(floor(total) != ceil(total)) returns true then total is not a perfect square, return false.
Return true otherwise.
Function perfect_square(int node, int root) takes a node and root node of a tree and returns the count of nodes in the given tree whose weight is a perfect square.
If if(check(Node_Weight[node])) returns true, then increment square.
Traverse tree in vector edge_graph[node] using for loop.
Call perfect_square(it, node) for the next node in the vector.
At the end of all functions we will have a square as the number of nodes with weights having value as a perfect square.
Example
#include <bits/stdc++.h> using namespace std; vector<int> Node_Weight(100); vector<int> edge_graph[100]; int square = 0; bool check(int check_it){ double total = sqrt(check_it); if(floor(total) != ceil(total)){ return false; } return true; } void perfect_square(int node, int root){ if(check(Node_Weight[node])){ square++; } for (int it : edge_graph[node]){ if(it == root){ continue; } perfect_square(it, node); } } int main(){ //weight of the nodes Node_Weight[2] = 121; Node_Weight[1] = 81; Node_Weight[4] = 37; Node_Weight[3] = 25; Node_Weight[8] = 100; Node_Weight[9] = 701; //create graph edge edge_graph[2].push_back(1); edge_graph[2].push_back(4); edge_graph[4].push_back(3); edge_graph[4].push_back(8); edge_graph[8].push_back(9); perfect_square(2, 2); cout<<"Count the nodes whose weight is a perfect square are: "<<square; return 0; }
Output
If we run the above code it will generate the following output −
Count the nodes whose weight is a perfect square are: 4
- Related Articles
- Count the nodes in the given tree whose weight is a power of two in C++
- Count of pairs in an array whose sum is a perfect square in C++
- Count the nodes in the given tree whose sum of digits of weight is odd in C++
- Count the nodes whose sum with X is a Fibonacci number in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- Is 9075 a perfect square?
- What is a perfect square?
- Count numbers upto N which are both perfect square and perfect cube in C++
- Count the nodes of the tree whose weighted string contains a vowel in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++
- Check if a number is perfect square without finding square root in C++
- Find the least $6$ digit which is a perfect square.
- Find the smallest number by which 1152 must be divided so that it becomes a perfect square. Also find the number whose square is the resulting number.
- Find the greatest five-digit number, which is a perfect square.
