Find All Distinct Subsets of a Given Set in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:17:40

4K+ 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

Print Nodes in the Top View of Binary Tree Using C++

Ayush Gupta
Updated on 01-Nov-2019 06:15:48

265 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

Find All Distinct Subset or Subsequence Sums of an Array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:14:35

481 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

Print Nodes Between Two Given Level Numbers of a Binary Tree Using C++

Ayush Gupta
Updated on 01-Nov-2019 06:12:35

186 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

Find All Combinations That Add Up to a Given Number Using C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:06:46

514 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

Minimum Number of Power Terms with Sum Equal to N Using C++

Narendra Kumar
Updated on 31-Oct-2019 07:40:40

323 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

Minimum Number of Points to Remove for Axis Alignment in C++

Narendra Kumar
Updated on 31-Oct-2019 07:37:14

600 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

Minimum Number of Platforms Required for a Railway Station Using C++

Narendra Kumar
Updated on 31-Oct-2019 07:35:09

349 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

Minimum Number of Palindromes Required to Express N as a Sum Using C++

Narendra Kumar
Updated on 31-Oct-2019 07:30:17

234 Views

Problem statementGiven a number N, we have to find the minimum number of palindromes required to express N as a sum of themIf N = 15 then 2 palindromes are required i.e. 8 and 7.Algorithm1. Generate all the palindromes up to N in a sorted fashion 2. Find the size of the smallest subset such that its sum is NExample#include #include #include #include using namespace std; vector table; int createPalindrome(int input, bool isOdd){    int n = input;    int palindrome = input;    if (isOdd)       n /= 10;    while (n > ... Read More

Minimum Number of Page Turns Using C++

Narendra Kumar
Updated on 31-Oct-2019 07:26:43

599 Views

Problem statementGiven a book of N pages, the task is to calculate the minimum number of page turns to get to a give desired page K.we can either start turning pages from the front side of the book (i.e from page 1) or from the backside of the book (i.e page number N).Each page has two sides, front and back, except the first page, which has only backside and the last page which may only have backside depending on the number of pages of the book.If N = 5 and K = 4 then we have to turn minimum 1 ... Read More

Advertisements