Found 26504 Articles for Server Side Programming

Maximum Depth of Binary Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:10:53

2K+ Views

Suppose we have one binary tree. We have to find the maximum depth of that tree. The maximum depth of a tree is the maximum number of nodes that are traversed to reach the leaf from the root using the longest path. Suppose the tree is like below. The depth will be 3 here.To solve this, we will follow these steps.Here we will use the recursive approach. The method is solve(root, depth = 0)if the root is empty, then return depthotherwise return max of solve(left, depth + 1) and solve(left, depth + 1)Let us see the following implementation to get ... Read More

Symmetric Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:05:44

721 Views

Suppose we have one binary tree. We have to check whether the tree is a symmetric tree or not. A tree will be said to be symmetric if it is the same when we take the mirror image of it. From these two trees, the first one is symmetric, but the second one is not.To solve this, we will follow these steps.We will call following steps recursively. The function will be solve(root, root)if the node1 and node2 are empty, then return trueif either node1 or node2 is empty, then return falsereturn true when node1.val = node2.val and solve(node1.left, node2.right) and ... Read More

Merge Sorted Array in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:05:12

2K+ Views

Suppose we have two sorted arrays A and B. We have to merge them and form only one sorted array C. The size of lists may different.For an example, suppose A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], then merged list C will be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]To solve this, follow these steps −define i := 0, j := 0 and end := length of A – 1while end >= 0 and not A[end], end := end – 1while j < length of Bif i > end ... Read More

Climbing Stairs in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:04:36

735 Views

There are n stairs. One person will go to 1st to nth stairs. Maximum how many stairs he/she can cross in one step is also given. With this information, we have to find possible ways to go to the nth stairs. Let us consider one can cross a maximum two stairs in each step. So we can find recursive relations to solve this problem. One can move to nth stair, either from (n-1)th stair or from (n-2)th stair. So ways(n) = ways(n-1) + ways(n-2).Suppose the number of stairs, say 10, the maximum number of stairs that can be jumped in ... Read More

Sqrt(x) in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:03:55

522 Views

Suppose we have a number x, and x is a non-negative number. We have to find the square root of x without using any library functions. So we have to create our own function to evaluate sqrt(x). In this function, the decimal digit of the output will be truncated.Suppose the value of x is 4, then the result will be 2 if the x is 8, then the result will be also 2, as sqrt(8) is 2.82842. But we will take only the integer part.To solve this, follow these steps −initialize l = 1, and h = x + 1, ... Read More

Plus One in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:02:33

2K+ Views

Suppose we have an array of integers, say A. A will hold n elements, and they are non-negative. The whole array A is representing one large number. So if A = [5, 3, 2, 4] is given, it indicates the number 5324. We have to take that array A, then increase the number by 1, and again return the number like an array as given. So after increasing A will be [5, 3, 2, 5]To solve this, we will follow these steps.Take the array and append each character into a string to make it stringthen convert the string to an ... Read More

Maximum Subarray in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:01:53

4K+ Views

Suppose we have an integer array A. We have to find the contiguous subarrays which length will be at least one, and that has the largest sum, and also return its sum. So if the array A is like A = [-2, 1, -3, 4, -1, 2, 1, -5, 4], then the sum will be 6. And the subarray will be [4, -1, 2, 1]To solve this we will try to use the Dynamic programming approach.define an array dp same as the size of A, and fill it with 0dp[0] := A[0]for i = 1 to the size of A ... Read More

Count and Say in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:00:49

1K+ Views

Here we will see the Count and Say sequence. This is a sequence whose few terms are like below −111211211111221The string will be read like1 (One)11 (One 1) So read the previous 1, and say “One 1”21 (Two 1) So read the previous 11, and say “Two 1”1211 (One 2 one 1) So read the previous 21, and say “One 2 one 1”111221 (One 1 one 2 two 1) So read the previous 1211, and say “One 1 one 2 two 1”Suppose we have a number n, 1

Implement strStr() in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:00:13

2K+ Views

Suppose we have two strings str and sub_str. We have to find the first occurrence of sub_str in the str. So if the string str is “helloworld”, and substring is “lo”, then the result will be 3.This can be done using the strstr() function in C. We have to design another function that is similar to the strstr() in C.To solve this, follow these steps −i := 0, j := 0, m := length of sub_str and n := length of strif m = 0, then return 0while i < n and n – i + 1 = m, doif ... Read More

Merge Two Sorted Lists in Python

Arnab Chakraborty
Updated on 28-Apr-2020 15:58:23

1K+ Views

Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may different.For an example, suppose A = [1,2,4,7] and B = [1,3,4,5,6,8], then merged list C will be [1,1,2,3,4,4,5,6,7,8]We will solve this using recursion. So the function will work like below −Suppose the lists A and B of function merge()if A is empty, then return B, if B is empty, then return Aif value in A

Advertisements