Found 33676 Articles for Programming

Program to find second deepest node in a binary tree in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:29:14

317 Views

Suppose we have a binary tree; we have to find the depth of the second deepest leaf. If there are multiple deepest leaves, the second deepest leaf node will be the next highest one. As we know the root has a depth of 0.So, if the input is likethen the output will be 1, as the second deepest node is 3.To solve this, we will follow these steps:if root is null, thenreturn nullnodes := a new listinsert root at the end of nodescount := 0, prev := 0, now := 0while nodes is not empty, donew := a new listflag ... Read More

Program to check whether leaves sequences are same of two leaves or not in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:21:57

114 Views

Suppose we have two binary trees; we have to check whether the sequence of leaves left-to-right in both trees are the same.So, if the input is likethen the output will be True as the sequence is [2, 6] for both trees.To solve this, we will follow these steps:c := a new listDefine a function inorder() . This will take root, and cif c is null, thenc := a new listif root is not null, theninorder(left of root, c)if left of root is null and right of root is null, theninsert value of root at the end of cinorder(right of root, ... Read More

Program to reverse words separated by set of delimiters in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:16:15

379 Views

Suppose we have a string and a set of delimiters, we have to reverse the words in the string while the relative ordering of the delimiters should not be changed.So, if the input is like s = "Computer/Network:Internet|tutorialspoint" delims = ["/", ":", '|'], then the output will be tutorialspoint/Internet:Network|ComputerTo solve this, we will follow these steps:words := a new listans := blank stringtemp := a map whereSeparate the words except the delimiter characters and insert them into words arrayseparate words when the character is in delimiter then add them into ans, otherwise read word from words array reversely and add ... Read More

Program to reverse inner nodes of a linked list in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:12:45

212 Views

Suppose we have linked list, we also have two values i and j, we have to reverse the linked list from i to jth nodes. And finally return the updated list.So, if the input is like [1, 2, 3, 4, 5, 6, 7, 8, 9] i = 2 j = 6, then the output will be [1, 2, 7, 6, 5, 4, 3, 8, 9, ]To solve this, we will follow these steps:prev_head := create a linked list node with value same as null and that points to nodeprev := prev_head, curr := nodeiterate through all values from 0 to ... Read More

Program to evaluate ternary expression in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:09:04

328 Views

Suppose we have an expression that holds the ternary expression, we have to evaluate the result of the expression. It supports some values like T and F for True and False and “?” and “:” characters. There are some properties:The length of the given string must be less than or equal to 10000.The conditional expressions group right-to-left.The condition will always be either T or F. So the condition will never be a digit.The result of the expression will always evaluate to T or F.So for example, if the input is like “T ? T ? F : T : T”, ... Read More

Program to check we can update a list index by its current sum to reach target or not in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:04:42

135 Views

Suppose we have a list of numbers called target. Now let us consider a list X with the same length as given list and X is filled with 1s. We can perform the following operation as many times as we want: Take any index i in X and set X[i] to the current sum of X. Finally check whether X can be turned into target or not.So, if the input is like target = [5, 9, 3], then the output will be True as initially X = [1, 1, 1], then update it with total sum 3, array will be ... Read More

Program to find string after deleting k consecutive duplicate characters in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:02:09

1K+ Views

Suppose we have a string s and another value k, we repeatedly delete the earliest k consecutive duplicate characters, and return the final string.So, if the input is like s = "paaappmmmma" k = 3, then the output will be "ma", as when we delete three "a"s to get "pppmmmma". Then we delete three "p"s to get "mmmma". Then delete three of the four "m"s to get "ma".To solve this, we will follow these steps:do the following steps infinitely, docount := 0chars := get the unique characters from sfor each character c in chars, doif k consecutive c is in ... Read More

Program to convert gray code for a given number in python

Arnab Chakraborty
Updated on 26-Nov-2020 06:59:12

922 Views

Suppose we have a number n, we have to find the gray code for that given number (in other words nth gray code). As we know the gray code is a way of ordering binary numbers such that each consecutive number's values differ by exactly one bit. Some gray codes are: [0, 1, 11, 10, 110, 111, and so on]So, if the input is like n = 12, then the output will be 10 as the 12 is (1100) in binary, corresponding gray code will be (1010) whose decimal equivalent is 10.To solve this, we will follow these steps:Define a ... Read More

Program to recover shuffled queue of people in python

Arnab Chakraborty
Updated on 26-Nov-2020 06:57:07

158 Views

Suppose we have a 2D matrix where each row contains two values [height, count] these indicates the person has given height and there are 'count' number of people in front of them that are at least as tall as them. Now consider this queue is shuffled, we have to recover the original ordering of the queue.So, if the input is like224050then the output will be405022To solve this, we will follow these steps:N := row count of matrixrearrange matrix rows based on increasing height and decreasing countans := make a list of size N and initially all entries are nullfor each ... Read More

Program to find number of distinct quadruple that forms target sum in python

Arnab Chakraborty
Updated on 26-Nov-2020 06:52:43

380 Views

Suppose we have four lists of numbers A, B, C, and D, and also have a target value, we have to find the number of distinct quadruple (i, j, k, l) such that A[i] + B[j] + C[k] + D[l] is same as target.So, if the input is like A = [5, 4, 3] B = [8, 4] C = [6, 2] D = [4, 10] target = 23, then the output will be 3, the quadruples are [5, 8, 6, 4] [3, 4, 6, 10] [3, 8, 2, 10].To solve this, we will follow these steps:count := 0m := ... Read More

Advertisements