Server Side Programming Articles - Page 1604 of 2646

Check if a given Binary Tree is height balanced like a Red-Black Tree in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:09:19

182 Views

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

Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:04:35

505 Views

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

Find the Largest Cube formed by Deleting minimum Digits from a number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:59:58

199 Views

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

Find the largest Complete Subtree in a given Binary Tree in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:56:20

324 Views

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

Find a pair from the given array with maximum nCr value in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:50:19

228 Views

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

What happens when more restrictive access is given to a derived class method in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:25:03

139 Views

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

Minimum removals from array to make GCD Greater in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:45:31

231 Views

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

What happens when a virtual function is called inside a non-virtual function in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:43:40

347 Views

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

Find three element from different three arrays such that that a + b + c = sum in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:41:39

230 Views

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

Minimum edges required to add to make Euler Circuit in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:40:55

243 Views

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

Advertisements