Found 7347 Articles for C++

Construct BST from given preorder traversal - Set 1 in C++

Arnab Chakraborty
Updated on 03-Jan-2020 11:36:03

96 Views

Suppose we have one pre order traversal. From this traversal. we have to generate the tree So if the traversal is like [10, 5, 1, 7, 40, 50], then the tree will be like −To solve this, we will use this trick. The trick is to set a range {min… max} for each node. At first we will initialize the range as {INT_MIN… INT_MAX}. The first node will definitely be in range, so after that we will create root node. To construct the left subtree, set the range as {INT_MIN… root->data}. If a values is in the range {INT_MIN… root->data}, ... Read More

Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}

Arnab Chakraborty
Updated on 03-Jan-2020 11:31:12

4K+ Views

Here we will see how to make a Turing machine for language L = {WWr |W belongs to {0, 1}}. So this represents a kind of language where we will use only two characters 0s and 1s. The w is a string and wr is reverse of it. So if w = 10110, then wr will be 01101. So the Turing machine will accept the string z = 1011001101.To solve this, we will use this approach. First check the first symbol, if it’s 0 then replace it using y and if that is 1, then replace using x. Then go ... Read More

Construct a Turing Machine for language L = {ww | w ∈ {0,1}}

Arnab Chakraborty
Updated on 03-Jan-2020 11:29:23

4K+ Views

Here we will see how to make a Turing machine for language L = {WW |W belongs to {0, 1}}. So this represents a kind of language where we will use only two characters 0s and 1s. The w is a string. So if w = 10110, so the Turing machine will accept the string z = 1011010110.To solve this, we will use this approach. The first thing is to find the midpoint of the string, we will convert a 0 to x and 1 to y. After continuously doing it a point is reached when all 0’s and 1’s ... Read More

Construct a Turing Machine for language L = {0n1n2n | n≥1}

Arnab Chakraborty
Updated on 03-Jan-2020 11:28:08

3K+ Views

Here we will see how to make a Turing machine for language L = {0n1n2n | n ≥ n}. So this represents a kind of language where we will use only three characters 0s, 1s and 2s. The w is a string. So if w = 000111222, The Turing machine will accept it.To solve this, we will use this approach. First replace one 0 from front by x, then keep moving right till we get one 1 and replace this 1 by y. Again, keep moving right till we get one 2, replace it by z and move left. Now ... Read More

Construct a Turing machine for L = {aibjck | i>j>k; k ≥ 1}

Arnab Chakraborty
Updated on 03-Jan-2020 11:26:31

443 Views

Here we will see how to make a Turing machine for language L = {AiBjCk | i > j > k; k ≥ 1}. So this represents a kind of language where we will use only three characters a, b and c. The w is a string. So if w = aaaaaabbbbccc, The Turing machine will accept it.To solve this, we will use this approach. Firstly compare two elements by making A and D as a single element, after that comparing A and D if count of C is greater than |(A, D)|, then the string will not be accepted, ... Read More

Construct a Turing machine for L = {aibjck | i< j< k; i ≥ 1}

Arnab Chakraborty
Updated on 03-Jan-2020 11:24:50

903 Views

Here we will see how to make a Turing machine for language L = {AiBjCk | i < j < k; i ≥ 1}. So this represents a kind of language where we will use only three characters A, B and C. The w is a string. So if w = AABBBBCCCCC, The Turing machine will accept it.To solve this, we will use this approach. Firstly compare two elements as a single element, after that comparing the single element if |first| > |(Second, Third)|, and |Second| > |Third|, then it will be accepted. Now if |Third| > |(First, Second)| and ... Read More

Construct a Turing machine for L = {aibjck | i*j = k; i, j, k ≥ 1}

Arnab Chakraborty
Updated on 03-Jan-2020 11:23:30

1K+ Views

Here we will see how to make a Turing machine for language L = {AiBjCk | i * j = k; i, j, k ≥ 1}. So this represents a kind of language where we will use only three characters A, B and C. The w is a string. So if w = AABBBBCCCCCCCC, The Turing machine will accept it.To solve this, we will use this approach.First replace an A with x and move right. Then skip all the A’s and move rightWhen the head reach to the first B then replace one B with y, then move right skipping ... Read More

Construct a graph from given degrees of all vertices in C++

Arnab Chakraborty
Updated on 03-Jan-2020 11:21:57

233 Views

Suppose we have a list of vertices, and their degrees are given. We have to generate one undirected graph from that degree sequence. It will not include loop or multiple edges. So if the degree sequence is like [2, 2, 1, 1], then the graph can be likeTo solve this, we will follow these steps −Define adjacency matrix adj to store the graphfor each vertex i, dofor each vertex j that is valid, and next to iif the degree of vertex i and j are more than zero, then connect themdisplay the matrix.Example Live Demo#include #include using namespace std; ... Read More

Construct a frequency array of digits of the values obtained from x^1, x^2, ....., x^n in C++

Arnab Chakraborty
Updated on 03-Jan-2020 11:18:25

73 Views

Suppose we have two integers x and n. We have to find the array such that it contains the frequency of index numbers occurring in (x^1, x^2, … x^(n – 1), x^n). So if x = 15 and n = 3, then output will be [0, 1, 2, 2, 0, 3, 0, 1, 0, 0]. As we know that x^1 to x^n, the values are 15, 225 and 3375. So the frequency array is 0, 1, 2, 2, 0, 3, 0, 1, 0, 0To solve this, we will follow these steps −Maintain the frequency count array to store the counts ... Read More

Why is a[i] == i[a] in C/C++ arrays?

Arnab Chakraborty
Updated on 03-Jan-2020 11:15:27

236 Views

Here we will see one amazing trick in C or C++. The array subscript A[i] can also be written as i[a]. In C/C++ E1[E2] is defined as (*((E1) + (E2))). The compiler performs arithmetic internally to access the array elements. Because of the conversion of rules, that is applied to the binary + operator, if E1 is an array object, and E2 is an integer, then E1[[E2] signifies the E2th element in the E1 array. So A[B] can be defined as *(A + B), so B[A] = *(B + A). so they are basically the same thing.Example Live Demo#include using ... Read More

Advertisements