Server Side Programming Articles - Page 1693 of 2650

Check if a given string is a valid number in C++

Arnab Chakraborty
Updated on 23-Jul-2020 07:51:03

531 Views

ConceptIt should be validated if a given string is numeric.Input − str = "12.5"Output − trueInput − str = "def"Output − falseInput − str = "2e5"Output − trueInput − 10e4.4Output − falseMethodWe have to handle the following cases in the code.We have to ignore the leading and trailing white spaces.We have to ignore the ‘+’, ‘-‘ and’.’ at the start.We have to ensure that the characters in the string belong to {+, -, ., e, [0-9]}We have to ensure that no ‘.’ comes after ‘e’.A digit should follow a dot character ‘.’.We have to ensure that the character ‘e’ should ... Read More

Check if a given Binary Tree is Heap in C++

Arnab Chakraborty
Updated on 23-Jul-2020 07:43:58

386 Views

ConceptWith respect of a given binary tree, we need to verify whether it has heap property or not, Binary tree need to satisfy the following two conditions for being a heap –Binary tree should be a complete tree (i.e. all levels except last should be full).Binary tree's every node’s value should be greater than or equal to its child node (considering max-heap).ExampleWith respect of following example this tree contains heap property –The following example does not have heap property –MethodIt is required to verify each of the above condition separately, for verifying completeness isComplete(This function checks if the binary tree ... Read More

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

Arnab Chakraborty
Updated on 23-Jul-2020 07:40:25

255 Views

ConceptWith respect of a Red-Black Tree, the largest height of a node is at most double the minimum height.For a given Binary Search Tree, we need to verify for 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.Examples13    41 \    / \ 15  11 101 \   /    \ 17 61 151Above tree cannot be a Red-Black Tree Above tree can be Red-Black Tree with any color assignmentMax height of 13 is 1Min height of 13 is 3  ... Read More

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

Arnab Chakraborty
Updated on 23-Jul-2020 07:38:45

458 Views

ConceptWith respect of given a string str containing characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, the task is to find if brackets are balanced or not.Brackets are denoted as balanced if −We close open brackets must be closed by the same type of brackets.Again we close open brackets according to the correct order.Input − str = “(()){}”Output − YesInput − str = “))(([][”Output − NoMethodAssign two variables a and b to keep track of two brackets to be compared.A count should be maintained whose value increments on encountering opening bracket and decrements on encountering a closing bracket.Assign b = ... Read More

Flood fill algorithm using C graphics

Arnab Chakraborty
Updated on 23-Jul-2020 07:34:42

5K+ Views

ConceptWith respect of a given rectangle, our task is to fill this rectangle applying flood fill algorithm.Input rectangle(left = 50, top = 50, right= 100, bottom = 100) floodFill( a = 55, b = 55, NewColor = 12, OldColor = 0)Output Method// A recursive function to replace previous color 'OldColor' at '(a, b)' and all surrounding pixels of (a, b) with new color 'NewColor' and floodFill(a, b, NewColor, OldColor)If a or b is outside the screen, thenreturn.If color of getpixel(a, b) is same asOldColor, thenRecur for top, bottom, right and left.floodFill(a+1, b, NewColor, OldColor);

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

Arnab Chakraborty
Updated on 23-Jul-2020 07:32:46

194 Views

ConceptWith respect of given number N, our task is to determine the largest perfect cube that can be formed by deleting minimum digits (possibly 0) from the number. So any digit can be deleted from the given number to reach the target.A is called a perfect cube if A = B^3 for some integer B.It has been seen that If the number cannot be perfect cube print -1.ExampleLet N = 1025. It has been seen that if we delete 0 from the above number we will get 125 as remaining number, which is cube root of 5(5 * 5 * ... Read More

Find the largest Complete Subtree in a given Binary Tree in C++

Arnab Chakraborty
Updated on 23-Jul-2020 07:29:39

306 Views

ConceptWith respect of a given Binary Tree, the task is to determine the size of maximum complete sub-tree in the given Binary Tree.Complete Binary Tree – A Binary tree is treated as Complete Binary Tree if all levels are completely filled without possibly the last level and the last level has all keys as left as possible.It has been noted that all Perfect Binary Trees are Complete Binary tree but reverse in NOT true. It has been seen that if a tree is not complete then it is also not Perfect Binary Tree.Input       2      / \ ... Read More

Find LCA in Binary Tree using RMQ in C++

Arnab Chakraborty
Updated on 23-Jul-2020 07:24:02

186 Views

ConceptThe article explains a method to solving the problem of finding the LCA of two nodes in a tree by reducing it to a RMQ problem.Examples Lowest Common Ancestor (LCA) of two nodes a and b in a rooted tree T is defined as the node located farthest from the root that has both a and b as descendants.For example, according to below diagram, LCA of node D and node I is node B.We can apply so many approaches to solve the LCA problem. These approaches differ with respect of their time and space complexities.Range Minimum Query (RMQ) is applied on ... Read More

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

Arnab Chakraborty
Updated on 23-Jul-2020 07:17:14

205 Views

ConceptWith respect of given an array arr[] of n positive integers, the task is to determineelements arr[i] and arr[j] from the array such that arr[i]Carr[j] is at most possible. With respect of more than 1 valid pairs, print any one of them.Input arr[] = {4, 1, 2}Output 4 2 4C1 = 4 4C2 = 4 2C1 = 4 (4, 2) is the only pairs with maximum nCr.MethodnCr is treated as a monotonic increasing function, that is n+1Cr > nCr. We can apply this fact to get close to our answer; we will select the max n among all the given integers. In ... Read More

Minimum removals from array to make GCD Greater in C++

Arnab Chakraborty
Updated on 23-Jul-2020 07:09:21

301 Views

ConceptWith respect of given N numbers, the target is to determine the minimum removal of numbers such that GCD of the remaining numbers is larger than initial GCD of N numbers. If it is impossible to increase the GCD, print “NO”.Input b[] = {1, 2, 4}Output1After removing the first element, then the new GCD is 2, which is larger than the initialGCD i.e., 1.Input b[] = {6, 9, 15, 30}Output 3The initial gcd is 3, after removing 6 and 9 to obtain a gcd of 15 which is larger than 3. We can also remove 9 and 15 to get a gcd of ... Read More

Advertisements