Programming Articles - Page 1852 of 3366

Ways to multiply n elements with an associative operation in C++

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

152 Views

In this problem, we are given an integer n which is the number of elements. Our task is to create a program that counts the number of ways to multiply n elements with the associative operation.Associative operations return the same result irrespective of the manner the numbers are arranged.Let’s take an example to understand the problem, Input3Output12Explanation(x*(y*z)), (x*(z*y)), (y*(x*z)), (y*(z*x)), (z*(x*y)), (z*(y*x)), ((x*y)*z), ((y*x)*z), ((x*z)*y), ((z*x)*y), ((z*y)*x), ((y*z)*x).To solve this problem, we will try to find if there is any relation or any type of series that can be created so that we can generalize our results. Let’s see the ... Read More

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

251 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

202 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

110 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

206 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

176 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

719 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

506 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

Write a program that does not terminate when Ctrl+C is pressed in C

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

304 Views

In this problem, we have to create a program that does not terminate when ctrl+C is pressed. Instead, it prints“Ctrl + C cannot terminate the program”.For this, we can use signal handling. The signal SIGINT is created on pressing ctrl+c. To solve this problem, we will catch this signal and handle it.Program to show the implementation of our solution,Example#include #include void signalHandle(int sig_num) {    signal(SIGINT, signalHandle);    printf(" Ctrl + C cannot terminate the program");    fflush(stdout); } int main (){    signal(SIGINT, signalHandle);    while(!0)    return 0; }OutputCtrl + C cannot terminate the program

Woodall Number in C++

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

284 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

Advertisements