Server Side Programming Articles - Page 1900 of 2650

Binary Tree Preorder Traversal in Python

Arnab Chakraborty
Updated on 04-May-2020 06:38:33

1K+ Views

Suppose we have a binary tree. We have to return the preorder traversal of that tree. So if the tree is like −Then the preorder traversal will be: [3, 9, 20, 15, 7]To solve this, we will follow these steps −make empty lists called res and st.node := rootwhile node or st is not emptywhile node is not null, theninsert val of node into res, insert node into st and set node := left of nodetemp := last element of st, and delete last element of stif right of temp is available, thennode := right of tempreturn resLet us see ... Read More

Prime numbers after prime P with sum S in C++

sudhir sharma
Updated on 03-Feb-2020 10:53:23

367 Views

In this problem, we are given three numbers, sum S, prime P, and N. Our task is to find all N prime numbers greater than P whose sum is equal to S.Let’s take an example to understand our problemInput: N = 2, P = 5, S = 18 Output: 7 11 Explanation: Prime numbers greater than 5 : 7 11 13 Sum = 7 + 11 = 18To solve this problem, we have to find all prime numbers between P and S. And then find N prime numbers which sum up to S. For this we will use backtracking.Program to ... Read More

Linked List Cycle II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:37:34

377 Views

Consider we have a linked list, and we have to check whether there is any cycle or not. To represent the cycle in the given linked list, we will use one integer pointer called pos. This pos represents a position in the linked list where tail is connected. So if pos is -1, then there is no cycle present in the linked list. For example, the linked list is like [5, 3, 2, 0, -4, 7], and pos = 1. So there is a cycle, and tail is connected to the second node. The constraint is that we cannot modify ... Read More

Prime numbers and Fibonacci in C++

sudhir sharma
Updated on 03-Feb-2020 10:49:44

819 Views

In this problem, we are given a number n. Our task is to print all prime and Fibonacci numbers less than or equal to n.Let’s take an example to understand the problemInput: n = 30 Output: 2 3 5 13ExplanationFibonacci numbers less than 30 are : 1 1 2 3 5 8 13 21.Out of these numbers, prime numbers are 2 3 5 13.To solve this problem, we have to check if all numbers of the Fibonacci series less than n is a prime number.For this, we will find all prime numbers less than or equal to n. And check ... Read More

Prime points (Points that split a number into two primes) in C++

sudhir sharma
Updated on 03-Feb-2020 10:39:59

192 Views

In this problem, we are given a number N. Our task is to print all prime points of the number otherwise print -1, if there is no prime point.Prime points are those index values which split the number into two prime numbers, one on the left and other on the right.Let’s take an example to understand the problemInput: 2359 Output: 1Explanation: on splitting the number at index 1. We will get 2 and 59 as two prime numbers.To solve this problem, we will check if there are left-right divisions possible for the number. If it’s valid, we will try all ... Read More

Construct Binary Tree from Inorder and Postorder Traversal in Python

Arnab Chakraborty
Updated on 04-May-2020 06:32:22

260 Views

Suppose we have the inorder and postorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the postorder and inorder sequences are [9, 15, 7, 20, 3] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps -Suppose the method is called buildTree with preorder and inorder listsroot := last node from the postorder, and delete first node from postorderroot_index := index of root.val from the inorder listleft or root := buildTree(subset of inorder from root_index + 1 to end, postorder)right or root := buildTree(subset of ... Read More

Prime String in C++

sudhir sharma
Updated on 03-Feb-2020 10:35:33

432 Views

In this problem, we are given a string. Our task is to print YES / NO based on is the sum of ASCII values of the characters of the string is prime or not.ASCII values are character encodingsPrime number is a number that is divisible only by the number itself and 1.Let’s take an example to understand the problem, Input: string = “Hello” Output:NoTo solve this problem, we will have to find the sum of ASCII values of all characters of the string. And store the sum in a variable and then check if the sum is a prime number ... Read More

Construct Binary Tree from Preorder and Inorder Traversal in Python

Arnab Chakraborty
Updated on 04-May-2020 06:27:14

1K+ Views

Suppose we have the inorder and preorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the preorder and inorder sequences are [3, 9, 20, 15, 7] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps −Suppose the method is called buildTree with preorder and inorder listsroot := first node from the preorder, and delete first node from preorderroot_index := index of root.val from the inorder listleft or root := buildTree(preorder, subset of inorder from 0 to root_index)right or root := buildTree(preorder, subset of inorder ... Read More

Reverse Linked List II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:24:06

268 Views

Suppose we have a linked list. We have to reverse the nodes from position m to n. We have to do it in one pass. So if the list is [1, 2, 3, 4, 5] and m = 2 and n = 4, then the result will be [1, 4, , 3, 2, 5]Let us see the steps −There will be two methods, the reverseN() and reverseBetween(). The reverseBetween() will work as main method.define one link node pointer called successor as nullThe reverseN will work as follows −if n = 1, then successor := next of head, and return headlast ... Read More

Prime Triplet in C++

sudhir sharma
Updated on 03-Feb-2020 10:33:02

355 Views

In this problem, we are given a number N. Our task is to print all prime triplets less than N.Prime triplet is a set of three prime numbers. That are of the form (p, p+2, p+6) or (p, p+4, p+6). All prime numbers are grouped according to the above triplets as every third prime in the direct pattern is a multiple of 6.Let’s see an example to understand the problemInput: N = 13 Output: 5 7 11To solve this problem, we have to find all prime numbers less than equal to N. And the check for triplets.The code to show ... Read More

Advertisements