Found 7347 Articles for C++

Print all interleavings of given two strings in C++

sudhir sharma
Updated on 22-Jan-2020 11:25:19

376 Views

In this problem, we are given two string str1 and str2 and we have to print all interleaving strings from both the string.Interleaving string created using two given strings such that the order of characters of each string.Let’s take an example to understand the problem −Input: str1 = “XY” str2= “NS” Output: XYNS, XNYS, XNSY, NXYS, NXSY, NSXYTo solve this problem, we will take all the characters in the strings. Length of str1 = m and length of str2 = n so we will create all interleaved strings from these strings.For printing all interleaving strings, we will fix characters of ... Read More

Print all internal nodes of a Binary tree in C++

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

1K+ Views

In this problem, we are given a binary tree and we have to print all internal nodes of the binary tree.The binary tree is a tree in which a node can have a maximum of 2 child nodes. Node or vertex can have no nodes, one child or two child nodes.Example −Internal Node is a node that can have at least one child i.e. non-leaf node is an internal node.Let’s take an example to understand the problem −Output − 7 4 9To solve this problem, we will traverse the binary tree using BFS(breadth-first search) traversal.While traversal we will push nodes ... Read More

Print all Jumping Numbers smaller than or equal to a given value in C++

sudhir sharma
Updated on 22-Jan-2020 11:18:45

755 Views

In this problem, we are given a number n and we have to print all jumping numbers that are smaller than or equal to n.Jumping Numbers are the number whose adjacent digits differ by one only. Some jumping numbers are 4565, 98, 7. All single-digit numbers are considered as jumping numbers. 235 is not a jumping number.Now, let’ take an example to understand the problemInput: N = 32 Output: 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32To solve this problem, we will assume a graph where 0 is the starting node and traverse ... Read More

Print all k-sum paths in a binary tree in C++

sudhir sharma
Updated on 22-Jan-2020 11:16:03

383 Views

In this problem, we are given a binary tree and a number K and we have to print all paths in the tree which have the sum of nodes in the path equal k.Here, the path of the tree can start from any node of the tree and end at any node. The path should always direct from the root node to the leaf node. The values of the nodes of the tree can be positive, negative, or zero.Let’s take an example to understand the problem −K = 5Output −1 3 1 3 2 1 4To solve this problem, we ... Read More

Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++

sudhir sharma
Updated on 22-Jan-2020 11:11:47

227 Views

In this problem, we are given a binary tree and we have to print all leaf nodes of the binary tree from left to right the iterative approach.Let’s take an example to understand the problemInput −Output − 1 4 7To solve this problem using the iterative approach, we will use a Depth-first search(DFS). To Traverse tree, we will start from root node and check if it is a leaf node if it is then print the node else find its child trees and traverse the child subtrees to find all leaf nodes.ExampleThe below code will implement our solution − Live Demo#include ... Read More

Print all leaf nodes of a binary tree from right to left in C++

sudhir sharma
Updated on 22-Jan-2020 11:04:51

488 Views

In this problem, we are given a binary tree and we have to print all leaf nodes of the binary tree from right to left.Let’s take an example to understand the problemInput −Output − 7 4 1To solve this problem, we will have to traverse the binary tree. This traversal can be done in two ways −Preorder traversal − This traversal uses recursion. Here, we will traverse, root then left and then right subtree. If we encounter a leaf node then we will print it, else we check for children of the node and explore them to find leaf node.ExampleProgram ... Read More

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

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

450 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

61 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

121 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

347 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

Advertisements