- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7347 Articles for C++
185 Views
In this tutorial, we will be discussing a program to print the path from root to a given node in a binary tree.For a given binary tree having distinct nodes, we have to print the complete path to reach a particularly given node from the root node of the binary tree.To solve this problem, we will use recursion. While traversing the binary tree, we will recursively search for the particular element to be found. Also alongside we will be storing the path to reach the element to be searched.Example#include using namespace std; struct Node{ int data; Node ... Read More
105 Views
In this tutorial, we will be discussing a program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are coprime.In this, we will be given an integer N, we have to print N integers less than 109 such that no two consecutive numbers are coprime but a pair of 3 consecutive integers must be coprime.For example, let us say we have the integer 4. Then the numbers that follow both of the above-provided conditions are6 15 35 14Example#include using namespace std; #define limit 1000000000 #define MAX_PRIME 2000000 #define MAX 1000000 #define I_MAX ... Read More
3K+ Views
Here we will see how to display all distinct subsets of a given set. So if the set is {1, 2, 3}, then the subsets will be {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}. The set of all subsets is called power set. The power set has 2n elements.We will loop through 0 to 2n (excluding), in each iteration we will check whether the ith bit in the current counter is set, then print ith element.Example#include #include using namespace std; void showPowerSet(char *set, int set_length) { unsigned int size = pow(2, set_length); for(int counter = 0; counter < size; counter++) { cout
139 Views
In this tutorial, we will be discussing a program to print all the nodes that appear in the top view of a given binary tree.For a particular binary tree, a node appears in its top view if it is the very first node at its horizontal distance. Horizontal distance for the left node of a node x is x-1 and for the right node of node x is x+1.To solve this, we will do the level order traversal so that we get the topmost node for a particular level before the other nodes present at that level. Further, we will ... Read More
345 Views
Suppose we have a set of integers. Find distinct sum, that can be formed from the subset of the given sets and print them in an ascending order. The sum of array elements is small. Consider the array elements are like [1, 2, 3]. Output will be 0, 1, 2, 3, 4, 5, 6. The distinct subsets are {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}, the sum values are 0, 1, 2, 3, 3, 5, 4, 6.To solve this, we will use the dynamic programming approach. When the sum of given element is small, ... Read More
88 Views
In this tutorial, we will be discussing a program to print nodes between the two given level numbers of a binary tree.In this, we will be given a low level and a high level for a particular binary tree and we have to print all the elements between the given levels.To solve this we can use queue-based level traversal. While moving through inorder traversal we can have a marking node at the end of each level. Then we can go to each level and print its nodes if the marking node exists between the given levels.Example#include #include using ... Read More
381 Views
Suppose we have a positive number n. We have to find all combinations of positive numbers, that adds up to that number. Here we want only combinations, not the permutations. For the value n = 4, there will be [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4]We will solve this using recursion. We have an array to store combinations, and we will fill that array using recursive approach. Each combination will be stored in increasing order of elements.Example#include using namespace std; void getCombination(int arr[], int index, int num, int decrement) { if (decrement < 0) return; if (decrement == 0){ for (int i = 0; i < index; i++) cout
219 Views
Problem statementGiven two positive integer N and X. The task is to express N as a sum of powers of X (X0 + X1 +…..+ Xn) such that the number of powers of X should be minimum.Print the minimum number of power of N used to make the sum equal to N.If N = 15 and X = 3 then we need 3 powers of ‘3’ as follows −15 = (32 + 31 + 31)AlgorithmUse below formula to calculate final result −1. If x = 1, then answer will be n only (n = 1 + 1 +…. n times)s ... Read More
542 Views
Problem statementWe are given N points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis.If given input is {(10, 5), (-2, -5), (13, 8), (-14, 7)} then if we remove (-2, -5) then all remaining points are above X-axis.Hence answer is 1.Algorithm1. Finds the number of points on all sides of the X-axis and Y-axis 2. Return minimum from both of themExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct point{ int x, ... Read More
230 Views
Problem statementGiven arrival and departure times of all trains that reach a railway station, the task is to find the minimum number of platforms required for the railway station so that no train waits.We are given two arrays that represent arrival and departure times of trains that stop.For below input, we need at least 3 platforms −TrainArrival timeDeparture timeTrain-109:0009:15Train-209:3511:45Train-309:4011:05Train-411:0012:00Train-514:3018:15Train-618:0019:00Algorithm1. Sort arrival and departure time arrays in ascending order 2. Trace the number of trains at any time keeping track of trains that haves arrived, but not departedExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int getPlatformCount(int ... Read More