Found 26504 Articles for Server Side Programming

Evaluate Reverse Polish Notation in C++

Arnab Chakraborty
Updated on 31-Jan-2020 06:36:02

361 Views

Suppose we have a triangle. We have to find the minimum path sum from top to the bottom. In each step we can move to adjacent numbers on the row below.For example, if the following triangle is like[    [2],    [3, 4],    [6, 5, 7],    [4, 1, 8, 3] ]The minimum path sum from top to bottom is 11 (2 + 3 + 5 + 1 = 11).Let us see the stepsCreate one table to use in Dynamic programming approach.n := size of trianglefor i := n – 2 down to 0for j := 0 to idp[j] ... Read More

Binary Tree Level Order Traversal in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:19:26

3K+ Views

Suppose we have a binary tree. We have to traverse this tree using the level order traversal scheme. So if the tree is likeThe traversal sequence will be like − [10, 5, 16, 8, 15, 20, 23]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front ... Read More

Binary Tree Inorder Traversal in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:11:48

3K+ Views

Suppose we have a binary tree. We have to traverse this tree using the inorder traversal scheme without using recursion. So if the tree is likeThen the traversal will be [2, 5, 7, 10, 15, 20]To solve this, we will follow these steps −Create two array res and stack, set curr := rootRun one infinite loopwhile current is not nullpush curr into a stack, and set curr := left of currwhen the length of stack = 0, then return resnode := popped element from the stackinsert a value of node into rescurr := right of currExampleLet us see the following ... Read More

Decode Ways in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:06:16

1K+ Views

Suppose we have a message containing letters from A to Z is being encoded to numbers using the following mapping − 'A' → 1, 'B' → 2 ... 'Z' → 26. So if we have one non-empty string, containing only digits, then we have to find, in how many ways that can be decoded. So if the string is like “12”, then that can be made from “AB”, or “L”, so there are two possible ways. So the answer will be 2.To solve this, we will follow these steps −We will solve this using dynamic programming.n := length of sdp ... Read More

Gray Code in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:01:01

2K+ Views

As we know that the gray code is a binary numeral system where two successive values differ in only one bit. Suppose we have a non-negative integer n representing the total number of bits in the code. We have to print the sequence of gray code. A gray code sequence must begin with 0. So if the input is 2, then the result will be [0, 1, 3, 2], this is because gray of 0 is 00, gray of 1 is 01, gray of 2 is 11, and gray of 3 is 10.To solve this, we will follow these steps ... Read More

Partition List in C++

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

626 Views

Suppose we have a linked list and a value x. We have to make partitions. The partition it such that all nodes less than x comes before the nodes that are greater than or equal to x. We should preserve the original relative order of the nodes in each of these two partitions. So if the list is like [1, 4, 3, 2, 5, 2] and x = 3, then the output will be [1, 2, 2, 4, 3, 5]To solve this, we will follow these steps −Make dummy nodes d1 and d2, and initialize them with -1, create two ... Read More

Combinations in C++

Arnab Chakraborty
Updated on 27-Apr-2020 14:16:54

1K+ Views

Suppose we have two integers n and k. We have to find all possible combinations of k numbers out of 1 ... n. So if n = 4 and k = 2, then the combinations will be [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]To solve this, we will follow these steps −We will use recursive function to solve this. the function solve() is taking n, k, temp array and start. The start is initially 1. This will act likeif size of temp array = k, then insert temp into res array, and returnfor i := ... Read More

Sort Colors in Python

Arnab Chakraborty
Updated on 27-Apr-2020 14:12:40

3K+ Views

Suppose we have an array with n objects. These are colored red, white, or blue, sort them in place so that the objects of the same color are adjacent. So with the colors in the order red, white and blue. Here, we will use the numbers like 0, 1, and 2 to represent the color red, white, and blue respectively. So if the array is like [2,0,2,1,1,0], then the output will be [0,0,1,1,2,2]To solve this, we will follow these steps −set low := 0, mid := 0 and high := length of array – 1while mid

Spiral Matrix in C++

Arnab Chakraborty
Updated on 27-Apr-2020 14:09:34

5K+ Views

Suppose we have a matrix and we have to print the matrix elements in a spiral way. At first starting from the first row, print the whole content and then follow the last column to print, then the last row, and so on, thus it prints the elements in a spiral fashion. So if the matrix is like −123456789101112131415161718Then the output will be like [1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11 15 16]To solve this, we will follow these steps −currRow := 0 and currCol := 0while currRow and ... Read More

Pow(x, n) in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:51:18

2K+ Views

Suppose we have two inputs x and n. x is a number in range -100.0 to 100.0, and n is a 32-bit signed integer. We have to find x to the power n without using library functions.So if the given inputs are x = 12.1, n = -2, then output will be 0.00683To solve this, we will follow these steps −power := |n| and res := 1.0while power is not 0if last bit of power is 1, then res := res * xx := x * xif n < 0return 1 / resreturn resExample(Python)Let us see the following implementation to ... Read More

Advertisements