Found 7197 Articles for C++

Ways to paint N paintings such that adjacent paintings don’t have same colors in C++

sudhir sharma
Updated on 17-Jul-2020 11:32:40

240 Views

In this problem, we are given two integers n and m, where n is the number of paintings and m is the number of colors available. Our task is to create a program that will find the total number of ways in which we can paint the paintings in such a way that no to consecutive paintings have the same color.Let’s take an example to understand the problem, Inputn = 3, m =3Output12ExplanationP1 P2 P3 C1 C2 C3 C1 C3 C2 C1 C2 C1 C1 C3 C1 C2 C1 C2 C2 C3 C2 C2 C1 C3 C2 C3 C1 C3 ... Read More

Ways to paint stairs with two colors such that two adjacent are not yellow in C++

sudhir sharma
Updated on 17-Jul-2020 11:30:06

195 Views

We are given n stairs and 2 colors (red and yellow) with which these stairs are to be painted. Our task is to count the number of ways in which we can paint the stairs such that no two consecutive steps will be yellow-colored.Let’s take an example to understand the problem, Input3Output5ExplanationThe ways in which stairs can be painted are YRY, RYR, YRR, RRY, RRR. here R denotes red color, Y denotes yellow color.To solve this problem, let’s see the number of ways the stairs can be painted.N = 1, ways(1) = 2 : R, YN = 2, ways(2) = ... Read More

Ways to place items in n^2 positions such that no row/column contains more than one in C++

sudhir sharma
Updated on 17-Jul-2020 11:27:40

102 Views

In this problem, we are given an integer n such that there are n lines vertically and n horizontally that are placed such that there are n2 intersection between these lines. Our task is to find the total number of ways by which 4 items can be placed on these intersections insuch a way that no row and column contains more that one item.Let’s take an example to understand the problem, Inputn=4Output24ExplanationTo solve this problem, we will have to choose 4 horizontal lines from n lines that will have items which will be nC4. Now, every horizontal line has n ... Read More

Ways to remove one element from a binary string so that XOR becomes zero in C++

sudhir sharma
Updated on 17-Jul-2020 11:25:29

198 Views

In this problem, we are given a binary string. Our task is to count the total number of ways in which we can remove one element such that XOR becomes zero.Let’s take an example to understand the problem, Inputn = 11010Output3to solve this problem, we need the logic that if the number of 1’s is even then XOR of the string will be 0, otherwise, we need to remove one 1 from the string. We can remove any number of 0’s without affecting the XOR.Program to show the implementation of our solution, Example Live Demo#include #include using namespace std; int wayXorZero(string ... Read More

Ways to select one or more pairs from two different sets in C++

sudhir sharma
Updated on 17-Jul-2020 11:23:43

167 Views

In this problem, we are given two positive numbers n and m (n > 1;       x = (1LL * x * x) % p;    }    return res; } void calculate(int n){    fact[0] = inverseMod[0] = 1;    for (int i = 1; i

Ways to sum to N using array elements with repetition allowed in C++

sudhir sharma
Updated on 17-Jul-2020 11:19:38

708 Views

In this problem, we are given an array of integers and a number N. Our task is to count the total number of ways N can be generated by adding elements of the array. All combinations and repetitions are allowed.Let’s take an example to understand the problem, Inputarr = {1, 3, 5} N = 6Output8ExplanationThe ways are −5+1, 1+5, 3+3, 3+1+1+1, 1+3+1+1, 1+1+3+1, 1+1+1+3, 1+1+1+1+1+1To solve this problem, we need to use a different approach as all types of combinations will be treated differently so, if the number is a sum of 4 elements of array 4 different ways are ... Read More

Ways to write N as sum of two or more positive integers in C++

sudhir sharma
Updated on 17-Jul-2020 11:16:05

495 Views

In this problem, we are given an integer n. Our task is to find the total number of ways in can be expressed as sum of two or more positive integers.Let’s take an example to understand the problem, InputN = 4Output5Explanation4 can be written as the sum in these ways, 4, 3+1, 2+2, 2+1+1, 1+1+1+1To solve this problem, we will use Euler’s recurrence formula. For a number n the total number of ways it can be generated p(n) by, Σ∞n=0 p(n)xn = Π∞k=1 (1/(1-xk ))Using this formula, we will derive formula for p(n), p(n) = p(n-1) + p(n-2) - p(n-5) ... Read More

Woodall Number in C++

sudhir sharma
Updated on 17-Jul-2020 11:06:50

262 Views

In this problem, we are given a number and our task is to check if the number is Woodall number or not.Woodall number is a special type of number which is of the form, Wn = n.2n -1First 5 Woodall numbers are 1, 7, 23, 63, 159Let’s take an example to understand the problem, InputX = 159OutputYesTo solve this problem, we will observe the number, if the number is even then it cannot be Woodall and then check for the number. To check, add the number by 1 and recursively divide the number by 2. after each division count the ... Read More

Word formation using concatenation of two dictionary words in C++

sudhir sharma
Updated on 17-Jul-2020 11:04:16

226 Views

In this problem, we are given a dictionary and a word. Our task is to check if the given wors can be formed using the concatenation of two dictionary words.While forming given words repetition of words is not legal.Let’s take an example to understand the problem, Inputdictionary = {“hello”, “tutorials”, “program” , “problem”, “coding”, “point”} word = “tutorialspoint”OutputyesExplanationtutorialspoint is created using tutorials and point.To solve this problem, we will store all elements of the dictionary in a prefix tree commonly known as a trie. And then search for the prefix of the word in the trie, if found split it ... Read More

Word Ladder (Length of shortest chain to reach a target word) in C++

sudhir sharma
Updated on 17-Jul-2020 11:00:30

322 Views

In this problem, we are given a dictionary and two words ‘start’ and ‘target’. Our task is to generate a chain (ladder) from start work to target word, the chain is created such that each word differs the other character by only one word and the word should also exist in the dictionary. The target word exists in the dictionary and also the length of all words is the same. The program will return the length of the shortest path from start to target.Let’s take an example to understand the problem, InputDictionary = {‘HEAL’, ‘HATE’, ‘HEAT’, ‘TEAT’, ‘THAT’, ‘WHAT’ , ... Read More

Advertisements