Suppose we have a number n, we have to check whether n is a Trojan Number or not. As we know that the Trojan Number is a number that is a strong number without a perfect power. A number n is a strong number when for every prime divisor or factor p of n, p^2 is also a divisor. In other words, every prime factor appears at least twice. Trojan numbers are strong. But the reverse is not true. So it means, not all strong numbers are Trojan numbers: only those that cannot be represented as a^b.So, if the input ... Read More
In this section we will discuss about where the variables and objects are stored in the memory when a C++ program is compiled. As we know, there are two parts of memory in which an object can be stored −Stack − All members that are declared inside block of memory, it stores inside the stack section. The main function is also a function, so elements inside it will be stored inside the stack.Heap − When some objects are allocated dynamically, then that is stored inside the heap section.The scope of the object declared inside a block or a function is ... Read More
Suppose we have n different points (Xi, Yi) in 2D coordinate and each point has a weight Wi, we have to check whether a line at 45 degree can be drawn. So that the sum of weights of points on each side will be same.So, if the input is like[[-1, 1, 3], [-2, 1, 1], [1, -1, 4]], then the output will be True/To solve this, we will follow these steps −n := size of vDefine one map weight_at_xmax_x := -2000, min_x := 2000for initialize i := 0, when i < n, update (increase i by 1), do −temp_x := ... Read More
Suppose we have one infinite chessboard with the same rules as that of chess and if there are N knights coordinates on the infinite chessboard and the king’s coordinate, we have to check whether the King is checkmate or not. The coordinate of infinite board is bounded by large value like (-10^9
In this section we will discuss about interesting facts restrictive access of derived class methods in C++. We will see some examples and analyze the output to know more about restrictions of using derived class methods in C++.Example (C++)Let us see the following implementation to get better understanding −#include using namespace std; class BaseClass { public: virtual void display(){ cout
Suppose we have an array A with N elements, we have another value p and a segment size k, we have to check whether key p is present in every segment of size k in A.So, if the input is like A = [4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4], p = 4 and k = 3, then the output will be TrueTo solve this, we will follow these steps −i := 0while i < n is non-zero, doj := 0while j < k, doif arr[j + i] is same as p, thenbreakj := j ... Read More
Suppose we have a string, that holds numeric characters and decimal points, we have to check whether that string is representing a number or not. If the input is like “2.5”, output will be true, if the input is “xyz”, output will be false.To solve this, we will follow these steps −To solve this, we will use the string parsing technique of our programming language. We will try to convert string to number, if there is no exception, then that will be a number, otherwise not a number.ExampleLet us see the following implementation to get better understanding − Live Demodef isNumeric(s): ... Read More
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