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
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
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
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
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
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
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
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
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
Array and vectors are very important data structures in competitive programming for solving problems. And the STL (Standard Template Library) in c++ programming provide some functions to perform operations of arrays and vectors.Let’s see some of these functions in action, Finding sum, Min and Max of the array/vector − In STL there are function that helps you to find the sum, max, and min of the array/vector. Functions with there function, Finding sumaccumulate(startIndex, endIndex, initialSum)Greatest element of the array/vecto*max_element(startIndex, endIndex)Smallest element of array/vector*min_element(startIndex, endIndex)Program to perform operations on array −Example Live Demo#include using namespace std; int main(){ int array[] ... Read More