ConceptWith respect of a given array arr1[] with size of array N, one another key X and a segment size K, the task is to determine that the key X present in every segment of size K in arr1[].Input arr1[] = { 4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4} X = 4 K = 3Output YesThere are existence of 4 non-overlapping segments of size K in the array, {4, 6, 3}, {5, 10, 4}, {2, 8, 4} and {12, 13, 4}. 4 is present all segments.Input arr1[] = { 22, 24, 57, 66, 35, 55, 77, 33, 24, ... Read More
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
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
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
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
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);
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
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
Suppose we want to make a house on an empty land which reaches all buildings in the shortest amount of distance. We can only move four directions like up, down, left and right. We have a 2D grid of values 0, 1 or 2, where −0 represents an empty land which we can pass by freely.1 represents a building which we cannot pass through.2 represents an obstacle which we cannot pass through.So, if the input is like102010000000100then the output will be 7 as Given three buildings are present at (0, 0), (0, 4), (2, 2), and an obstacle is at ... Read More
Suppose we have one matrix; we have to find the length of the longest increasing path. From each cell, we can either move to four directions − left, right, up or down. We cannot move diagonally or move outside of the boundary.So, if the input is like994668211then the output will be 4 as the longest increasing path is [3, 4, 5, 6].To solve this, we will follow these steps −Define a function solve(). This will take i, j, matrixif dp[i, j] is non-zero, thenreturn dp[i, j]dp[i, j] := 1temp := 0for r in range i-1 to i+2, dofor c in ... Read More