Found 7197 Articles for C++

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

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

220 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

186 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

261 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

216 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

174 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

210 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++

Farhan Muhamed
Updated on 18-Aug-2025 11:30:02

304 Views

A Binary Search Tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. In this article, we will solve the problem of finding all odd nodes of a binary search tree in C++. Find Odd-Valued Nodes in BST You are given a binary search tree (BST) as input and your task is to write a program that finds all the nodes with odd values and returns them as output. To understand ... Read More

Print all pairs in an unsorted array with equal sum in C++

sudhir sharma
Updated on 22-Jan-2020 09:42:05

314 Views

In this problem, we have an unsorted array and we have to print all pairs within this array that have an equal sum.Let’s take an example to understand the problem −Input: array = [12, 13, 20, 5] Output: [12, 13] and [20, 5] have sum 25.To solve this problem, we will have to find pairs of the same sum. For this, we will check pairs for the same sum. And to avoid duplicate pairs, we will use the map.For this, we will need two maps, one to store all sum pairs and their sum and others to store all sum ... Read More

Print all pairs of anagrams in a given array of strings in C++

sudhir sharma
Updated on 22-Jan-2020 09:37:31

465 Views

In this problem, we are given an array of strings and we have to print all pairs of anagrams of that given array.Anagrams are strings that are formed by rearranging the character of another string. Like − hello and lolheLet’s take an example to understand the problem −Input: array = {“hello”, “hrdef”, “from”, “lohel”, “morf”}. Output: [hello, lohel] , [from , morf]To solve this problem, we will use the nesting of loops. We need two nested loops, the outer loop will traverse over the array and select elements. The nested loop will check each of the string and check if ... Read More

Convert time from 24 hour clock to 12 hour clock format in C++

Ayush Gupta
Updated on 22-Jan-2020 07:49:52

911 Views

In this tutorial, we will be discussing a program to convert time from 24 hour clock to 12 hour clock format.For this we will be provided with certain time in 24 hour format. Our task is to convert it into 12 hour format with the extension of “AM” or “PM”.Example Live Demo#include using namespace std; //converting into 12 hour format void convert_12hour(string str){    int h1 = (int)str[0] - '0';    int h2 = (int)str[1] - '0';    int hh = h1 * 10 + h2;    //finding the extension    string Meridien;    if (hh < 12) {       Meridien = "AM";    }    else       Meridien = "PM";       hh %= 12;    if (hh == 0) {       cout

Advertisements