C++ Articles

Page 527 of 597

Diagonal Traversal of Binary Tree in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 326 Views

To consider the nodes that are passing between lines of slope -1. The diagonal traversal of the binary tree will be to traverse and print all those nodes present between these lines.Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.struct Node {    int data;    struct Node *leftChild, *rightChild; };Next we create our createNode(int data) function that takes an int value and assign it to the data ...

Read More

Construct Pushdown automata for L = {0(n+m)1m2n | m, n = 0} in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 07-Jan-2021 648 Views

We are given with a language “L” and the task is to construct a pushdown automata for the given language which explains that the occurrences of 0’s will be the addition of occurrences of 1’s and 2’s and also, occurrence of 1 and 2 will be minimum one which can also makes the string NULL and it should be accepted by the automata.What is pushdown Automata?A pushdown automata or pushdown automaton or PDA is a technique to implement a context−free grammar in a similar way we design Deterministic Finite Automaton or DFA for a regular grammar. A DFA can operate ...

Read More

First non-repeating character using one traversal of string in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 29-Dec-2020 1K+ Views

In this tutorial, we are going to learn how to find the first non-repeating character in the given string. Let's see an example.Input −tutorialspointOutput −uLet's see the steps to solve the problem.Initialize the string.Initialize a map char and array to store the frequency of the characters in the string.Iterate over the string.Find the frequency of each character and store them in the map.Store the index of the character as well.Iterate over the character frequencies in the map.Print the first character with the frequency 1.ExampleLet's see the code.#include #include using namespace std; void findDistinctCharacters(string random_string) {    // initializing ...

Read More

Count of operations to make a binary string "ab" free in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 02-Dec-2020 380 Views

We are given a string which can contain “ab” and the task is to calculate the count of operations required to remove or delete “ab” from the string. So, our task is to firstly check whether the string contains “ab” or not if yes then we have to make string “ab” free.Input − string str = "ababaa"Output − Count of operations to make a binary string “ab” free are − 4Explanation − As we can see in the string “ab” pattern is occurring two times so we will replace “ab” with “bba” so the count of operations is 1 and ...

Read More

Count rotations of N which are Odd and Even in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 01-Dec-2020 239 Views

We are given a number N. The goal is to count the rotations of N that make an odd number and rotations that make an even number. If the number N is 123 its rotations would be 123, 321, 132. The odd rotations are 123 and 321 ( 2 ) and even rotation is 132 ( 1 ).Let us understand with examples.Input − N= 54762Output −Count of rotations of N which are Odd are − 2Count of rotations of N which are Even are − 3Explanation − Rotations are −54762, 25476, 62547, 76254, 47625.Even rotations are 3 − 54762, 25476, ...

Read More

Sparse Matrix Multiplication in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Nov-2020 3K+ Views

Suppose we have two matrices A and B, we have to find the result of AB. We may assume that A's column number is equal to B's row number.So, if the input is like [[1, 0, 0], [-1, 0, 3]] [[7, 0, 0], [0, 0, 0], [0, 0, 1]], 100-103700000001then the output will be [[7, 0, 0], [-7, 0, 3]]700-703To solve this, we will follow these steps −r1 := size of A, r2 := size of Bc1 := size of A[0], c2 := size of B[0]Define one 2D array ret of order r1 x c2Define an array sparseA[r1] of pairsfor ...

Read More

Maximum Sum of Two Non-Overlapping Subarrays in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 13-Nov-2020 301 Views

Suppose we have an array A of integers; we have to find the maximum sum of elements in two non−overlapping subarrays. The lengths of these subarrays are L and M.So more precisely we can say that, we have to find the largest V for whichV = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either −0 = 0, decrease i by 1, decrease j by 1, do −rightL[i + 1] := max of temp and x where x is 0 if i + 2 >= n otherwise x = rightL[i + 2]temp ...

Read More

Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 31-Oct-2020 269 Views

We are given two arrays containing prime and non prime numbers. The goal is to find the count of distinct sums of pairs of prime numbers in each array. We will do this by making a pair of two primes from each array, take their sum and add them to set sums. In the end the size of the set is the number of distinct sums of primes. Let's understand with examples. Input Arr1[] = { 1, 2, 3 } Arr2[] = { 2, 3, 4} Output Distinct Sums of primes :3 Explanation Prime pairs ...

Read More

Count numbers whose difference with N is equal to XOR with N in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 31-Oct-2020 190 Views

We are a number N. The goal is to find numbers between 0 and N whose difference with N is equal to XOR with N.We will do this by traversing no. from i=0 to i

Read More

Count pieces of the circle after N cuts in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 31-Oct-2020 176 Views

We are given an integer N which represents the number of cuts applied on a 2D-circle. Each circle divides the circle in two halves. Goal is to find the pieces of the circle after N cuts.Number of pieces= 2 * no. of cutsLet’s understand with examples.Input − N=1Output − Pieces of circle: 2Explanation −Input − N=3Output − Pieces of circle: 6Explanation −Approach used in the below program is as followsWe take N for a number of cuts.Take pieces=1*N.Print the result..Example#include using namespace std; int main(){    int N=2;    Int pieces=2*N;    cout

Read More
Showing 5261–5270 of 5,962 articles
« Prev 1 525 526 527 528 529 597 Next »
Advertisements