Suppose we have a binary tree; we have to check whether it is heap or not. The heap has following property: Heap will be a binary tree That tree should be a complete tree (So. all levels except last should be full). Every nodes value of that tree should be greater than or equal to its child node (max-heap).So, if the input is likethen the output will be trueTo solve this, we will follow these steps −Define a function number_of_nodes() . This will take rootif root is null, thenreturn 0otherwise, return(1 + number_of_nodes(root.left) + number_of_nodes(root.right))Define a function has_heap_property() . This ... Read More
Suppose there is a Red-Black Tree, here the largest height of a node is at most double the minimum height. If we have a binary search tree, we have to check the following property. With respect of every node, length of the longest leaf to node path has not more than double the nodes on shortest path from node to leaf.So, if the input is likethen the output will be True, as this is balanced.To solve this, we will follow these steps −Define a function solve() . This will take root, max_height, min_heightif root is null, thenmax_height := 0, min_height ... Read More
Suppose we have a string str containing these brackets '(', ')', '{', '}', '[' and ']', we have to check whether brackets are balanced or not. We can say brackets are balanced when Opening and closing bracket types are of same type. Brackets are closed in correct order.So, if the input is like {([])}, then the output will be True.To solve this, we will follow these steps −cnt := 0i := 0j := -1Define a function solve() . This will take s, tempcnt := cnt - 1s := a new list from sif j > -1 and s[j] is same ... Read More
Suppose we have a number N, we have to determine the largest perfect cube that can be generated by removing minimum digits (possibly 0) from the number. We can delete any digit from the given number to reach the target. As we know a number N is called a perfect cube if N = M^3 for some integer M.So, if the input is like 806, then the output will be 8, as we can delete 0 and 6 from the number, then we will get 8, this is perfect cube of 2.To solve this, we will follow these steps −Define ... Read More
Suppose we have a Binary Tree; we have to find the size of maximum complete sub-tree in this Binary Tree. As we know a complete binary tree is a Binary Tree if all levels are completely filled without possibly the final level and the final level has all keys as left as possible.So, if the input is likethen the output will be 4 as size and inorder traversal will be 10, 45, 60, 70, To solve this, we will follow these steps −Define return type with few parameters like isComplete, isPerfect, these are initially false, then size and rootTree, size ... Read More
Suppose we have an array arr with n integers, we have to find arr[i] and arr[j] from the array such that arr[i]Carr[j] is at large as possible. If there is more than one pair, return any one of them.So, if the input is like [4, 1, 2], then the output will be 4 2 as 4C1 = 4, 4C2 = 6 and 2C1 = 2, so (4, 2) is only pair as we want.To solve this, we will follow these steps −sort the list vN := v[n - 1]if N mod 2 is same as 1, thenfirst := N / ... Read More
Suppose we have a list of N numbers; we have to find the minimum number of removal of numbers are required so that the GCD of the remaining numbers is larger than initial GCD of N numbers.So, if the input is like [6, 9, 15, 30], then the output will be 2 as the initial gcd is 3, so after removing 6 and 9 we can get gcd as 15, 15 > 3.To solve this, we will follow these steps −INF := 100001spf := a list with elements 0 to INFDefine a function sieve()for i in range 4 to INF, ... Read More
In this section we will discuss about interesting facts about virtual classes in C++. We will see two cases first, then we will analyze the fact.At first execute the program without using any virtual function.The execute the program using any virtual function under non-virtual function.ExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; class BaseClass { public: void display(){ cout
Suppose we have three arrays A, B, C and another value called "sum", We have to check whether there are three elements a, b, c such that a + b + c = sum and a, b and c should be under three different arrays.So, if the input is like A = [2, 3, 4, 5, 6], B = [3, 4, 7, 2, 3], C = [4, 3, 5, 6, 7], sum = 12, then the output will be True as 4+2+6 = 12, and 4, 2, 6 are taken from A, B, C respectively.To solve this, we will follow ... Read More
Suppose we have an undirected graph of b number of nodes and a number of edges; we have to find minimum edges needed to build Euler Circuit in this graph.So, if the input is likethen the output will be 1.To solve this, we will follow these steps −Define a function dfs() . This will take g, visit, odd_vert, degree, comp, vvisit[v] := 1if degree[v] mod 2 is same as 1, thenodd_vert[comp] := odd_vert[comp] + 1for u in range 0 to size of g[v], doif visit[u] is same as 0, thendfs(g, visit, odd_vert, degree, comp, u)From the main method do the ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP