C++ Articles

Page 547 of 597

Print all the cycles in an undirected graph in C++

sudhir sharma
sudhir sharma
Updated on 17-Jan-2020 3K+ Views

In this problem, we are given an undirected graph and we have to print all the cycles that are formed in the graph. Undirected Graph is a graph that is connected together. All the edges of the unidirectional graph are bidirectional. It is also known as an undirected network. Cycle in a graph data structure is a graph in which all vertices form a cycle. Let's see an example to understand the problem better − Graph- Output- Cycle 1: 2 3 4 5 Cycle 2: 6 7 8 For this, we will make use of a few properties of ...

Read More

Print all the sum pairs which occur maximum number of times in C++

sudhir sharma
sudhir sharma
Updated on 17-Jan-2020 237 Views

In this problem, we are given an array of n unique integers. And we have to find the sum of two integers of the array which has the maximum frequency. The problem has multiple solutions and you need to find them all.Input : array = { 1, 12, 5, 7, 9, 11} Output : 16 12Explanation − sum 16 and 12 occur two times.5 + 11 = 16 & 7 + 9 = 16 1 + 11 = 12 & 5 + 7 = 12Now to solve this problem, our approach to the problem is checking the occurrence every sum ...

Read More

Maximum element in a very large array using pthreads in C++

Narendra Kumar
Narendra Kumar
Updated on 10-Jan-2020 583 Views

Problem statementGiven a very large array of integers, find maximum within the array using multithreadingExampleIf input array is {10, 14, -10, 8, 25, 46, 85, 1673, 63, 65, 93, 101, 125, 50, 73, 548} thenmaximum element in this array is 1673AlgorithmLet us call array size as total_elementsCreate N threadsEach thread will process (total_elementes/N) array elements and will find maximum element from it.Finally compute the maximum from the maximum value reported by each thread.Example#include #include #include #include #define MAX 10 #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) typedef struct struct_max {    int start;    int end;    int thread_num; } ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 03-Jan-2020 251 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 = {ww | w ∈ {0,1}}

Arnab Chakraborty
Arnab Chakraborty
Updated on 03-Jan-2020 8K+ 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
Arnab Chakraborty
Updated on 03-Jan-2020 6K+ 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; i, j, k ≥ 1}

Arnab Chakraborty
Arnab Chakraborty
Updated on 03-Jan-2020 2K+ 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

Top Reasons to Learn C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 03-Jan-2020 261 Views

Here we will see some good reasons behind taking the language C++ as our favorite programming language. We know that C++ is one of the most popular object oriented programming language. These are the reasons behind taking the C++ into consideration.C++ Popularity and High salary −C++ is one of the most popular language in the world. It is used nearly 4.4 million developers worldwide. The C++ developers hold highest paying jobs in the industry with an average base pay of $100000 per year.C++ has abundant library supportv −C++ has Standard Template Library (STL). This helps to write code compactly and ...

Read More

Print all ways to break a string in bracket form in C++

sudhir sharma
sudhir sharma
Updated on 03-Jan-2020 282 Views

In this problem, we are given a string and we have to break it into substrings and print them enclosing brackets.Let’s take a few examples to understand the problem better, Input : wxyz Output :    (w) (x) (y) (z)    (w) (x) (yz)    (w) (xy) (z)    (w) (xyz)    (wx) (y) (z)    (wx) (yz)    (wxy) (z)    (wxyz)Explanation − We will break the string into all possible substrings. And enclose each substring with brackets.Now, since we have understood the problem, let’s create a solution to the problem.Here, we will use recursion to solve the problem. ...

Read More

Print BST keys in the given range in C++

sudhir sharma
sudhir sharma
Updated on 03-Jan-2020 734 Views

In this problem, we are given two nodes of a binary search tree. And we have to print all the values in the range of k1 to k2 that occur in the tree. That is we have to print all the values that are greater than k1 and smaller than k2. And we have to print all these keys in increasing order of their values.Binary Search Tree is a tree which follows these 3 properties −The left subtree has nodes with values lesser than the node’s value.The right subtree has a node with values greater than the node’s value.A subtree’s ...

Read More
Showing 5461–5470 of 5,962 articles
« Prev 1 545 546 547 548 549 597 Next »
Advertisements