Found 7347 Articles for C++

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

167 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

267 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

401 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

143 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

103 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

179 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

Print all nodes that are at distance k from a leaf node in C++

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

144 Views

In this problem, we are given a binary tree and a number K. We have to print all nodes of the tree that are at k distance from the leaf node.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.In this problem, distance from the leaf node is the node at a higher level than the leaf node. Suppose, the node at distance 2 from the leaf node at level 4 will be at level 2.Let’s take an example to understand ... Read More

Print all numbers less than N with at-most 2 unique digits in C++

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

116 Views

In this problem, we are given an integer N and we have printed all the number less than N with at-most 2 unique digits i.e. maximum 2 different digits can be used to create the number.Let’s take an example to understand the problem −Input: N = 17 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16To solve this problem, we will be generating all numbers that have only two unique digits. Our number generating process starts from 0 and ends when our number is equal to or greater than N. For two ... Read More

Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++

sudhir sharma
Updated on 22-Jan-2020 09:55:23

161 Views

In this problem, we are given a set of N numbers and a number X. And we have to print all numbers from the array whose set of prime factors is a subset of the set of prime factors of X.Let’s take an example to understand the problemInput: X= 30 , array = {2, 3, 6, 10, 12} Output : 2 3 6To solve this problem, we have to traverse elements of the array. And divide this element with gcd of (element, x). Repeat division till the gcd becomes 1. And print the remaining number.Example Live Demo#include using namespace std; ... Read More

Print all odd nodes of Binary Search Tree in C++

sudhir sharma
Updated on 22-Jan-2020 09:51:54

198 Views

In this problem, we are given a binary search tree and we have to print all the nodes that have odd values.The binary search tree is a special type of tree that possess the following properties −The left subtree always has values smaller than the root node.The right subtree always has values larger than the root node.Both the left and right subtree should also follow the above two properties.Let’s take an example to understand the problem −Output − 1 3 9To solve this problem, a simple approach would be traversing the tree. On traversal, we will check the value of each ... Read More

Advertisements