Server Side Programming Articles - Page 1928 of 2646

Print all leaf nodes of an n-ary tree using DFS in C++

sudhir sharma
Updated on 22-Jan-2020 10:59:00

621 Views

In this problem, we are given a 2-D array containing the edge of an n-ary where edge defines the edge of the n-ary tree. We have to print all the leaf nodes of the created a-ary tree.The n-ary tree is a tree with has maximum n children i.e. a node can have 1, 2, ...n child nodes.Let’s see an example to understand the problem −Input: edge[][] = {{5, 8}, {5, 6}, {8, 1}, {8, 4}, {6, 7}} Output: 1 4 7Explanation − let's create a tree using the edge array −The leaf nodes of this tree are 1, 4, 7.To ... Read More

Print all multiplicative primes <= N in C++

sudhir sharma
Updated on 22-Jan-2020 10:55:19

129 Views

In this problem, we are given an integer n and we have to print all multiplicative primes less than or equal to n.Multiplicative primes are prime numbers that have a product of their digits also prime numbers. Like 2, 3, 5, 7, 13, 17.23 is prime but not a multiplicative prime because of 2*3 = 6.Let’s take an example to understand the problem −Input: n = 9 Output: 2 3 5 7To solve this problem, we will find all prime numbers less than n. And check if the number is multiplicative prime. And print all multiplicative prime less than n.ExampleThe ... Read More

Print all n digit patterns formed by mobile Keypad in C++

sudhir sharma
Updated on 22-Jan-2020 10:49:06

230 Views

In this problem, we are given a number n and we have to print all N digit patterns formed by pressing the mobile keypad button. While pressing the buttons, we can press only nearby buttons of the current button i.e. only keys left, right, up, down can be pressed.Let’s see how the old keypad looks like −12ABC3DEF4GHI5JKL6MNO7PQRS8TUV9WXYZ*0#Let’s take an example to understand the problemInput: n=2 Output: 12 14 21 23 25 32 36 41 45 47 52 54 56 58 63 65 69 74 78 85 87 89 80 96 98To solve this problem, we will use a depth-first search ... Read More

Print all n-digit numbers whose sum of digits equals to given sum in C++

sudhir sharma
Updated on 22-Jan-2020 10:43:04

500 Views

In this problem, we are given two numbers n and sum. We have to print all n digit numbers whose sum is equal to the sum. In this problem, numbers with leading 0’s are not considered.Let’s take an example to understand the problem, Input: n = 2 , sum = 5 Output: 14 23 32 41 50 Explanation: The sum of digits of the number in all numbers in 5.To solve this problem, we will have to find all the n-digit numbers with sum with the given sum value. For this, we will fix a digit place with all values ... Read More

Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++

sudhir sharma
Updated on 22-Jan-2020 10:40:04

263 Views

In this problem, we are given an integer n, and we have to print all n-digit numbers such that the absolute difference between the sum of digit of the number at even and odd places is 1. While creating the numbers leading 0’s are not considered.The absolute difference is the difference between both numbers whose value is an absolute value (positive value).Let’s take an example to understand the problem −Input: n = 2 Output: 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 Explaination : taking an of the numbers from the ... Read More

Print all n-digit strictly increasing numbers in C++

sudhir sharma
Updated on 22-Jan-2020 10:36:09

414 Views

In this problem, we are given a number N and we have to print all n-digit numbers whose digits are strickly increasing from MSB to LSB i.e. the number at LSB (left) should be smaller than the number at right.Let’s take an example to understand the problem −Input − n = 2Output −01 02 03 04 05 06 07 08 09 12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47 48 49 56 57 58 59 67 68 69 78 79 89.Explanation − as you ... Read More

Print all nodes at distance k from a given node in C++

sudhir sharma
Updated on 22-Jan-2020 10:32:32

505 Views

In this problem, we are given a binary tree, a target node and an integer K. We have to print all the nodes of the tree that are at a distance K from the target node.Binary Tree is a special tree whose each node has at max two nodes (one/two/none).Let’s take an example to understand the problemK = 2Target node: 9Output −5 1 3.Explanation −The distance can be taken for node a higher, lower or at the same level. So, we will return nodes accordingly.To solve this problem we have to understand what are the types of nodes that are ... Read More

Print all nodes between two given levels in Binary Tree in C++

sudhir sharma
Updated on 22-Jan-2020 10:23:32

250 Views

In this problem, we are given a binary tree and two levels in the tree (upper and lower) and we have to print all nodes between upper and lower levels of the tree.The binary tree is a special tree whose each node has at max two nodes (one/two/none).Let’s take an example to understand the problem −upper − 1lower − 3Output −6 3 9 7 4 8 10To solve this problem, we have to print nodes of the tree at a given level. We will call a recursive function using a loop from the upper to lower level in the tree.This algorithm is simple ... Read More

Print all nodes in a binary tree having K leaves in C++

sudhir sharma
Updated on 22-Jan-2020 10:17:13

207 Views

In this problem, we are given a binary tree and an integer K and we have to print all nodes of the binary tree that have K leaves in their child subtree.The binary tree is a special tree whose each node has at max two nodes (one/two/none).The leaf node of a binary tree is the node at end of the tree.Let’s take an example to understand the problem −K = 2Output − {S}To solve this problem, we will do traversal (postorder) for the tree. Now, we will see each left subtree and right subtree if the sum of leaves is ... Read More

Print all nodes less than a value x in a Min Heap in C++

sudhir sharma
Updated on 22-Jan-2020 10:13:49

283 Views

In this problem, we are given a Min Heap and a value x and we have to print all nodes less than x.Min heap is a special type of binary tree in which every node has a value less than the node value of its child node.Let’s take an example to understand the problem −X = 45Output − 2 4 7 10 17 22 33 34Now, to solve this problem we need to do pre-order traversal of the whole min-heap and print only those values which are less than the given value X. If a value of a node is ... Read More

Advertisements