Articles on Trending Technologies

Technical articles with clear explanations and examples

DFA for Strings not ending with “THE” in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 413 Views

To use Deterministic Finite Automaton(DFA) to find strings that aren’t ending with the substring “THE”. We should keep that in mind that any variation of the substring “THE” like “tHe”, “The” ,”ThE” etc should not be at the end of the string.First, we define our dfa variable and initialise it to 0 which keeps our track of state. It is incremented on each character matched.int dfa = 0;The begin(char c) method takes a character and checks if its ‘t’ or ‘T’ and go to first state i.e 1.void begin(char c){    if (c == 't' || c == 'T')   ...

Read More

DFA based division in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 303 Views

The Deterministic Finite Automaton(DFA) is used for checking if a number is divisible by another number k or not. The algorithm is useful because it can also find the remainder if the number isn’t divisible.In DFA based division we build a DFA table with k states. We consider binary representation of the number so there is only 0 and 1 in each state in DFA.The createTransTable(int k, int transTable[][2]) function is used for creating the transTable and storing the states in it. It takes the number k by which the number is to be divisible and transTable[][2] which is an ...

Read More

Determine the number of squares of unit area that a line will pass through in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 184 Views

The objective is to determine the number of squares a line will pass through given two endpoints (x1, y1) and (x2, y2).To find the number of squares through which our line pass we need to find : difference between the x points (dx) = x2-x1, difference between the y points (dy) = y2-y1, adding the dx and dy and subtracting by their gcd (result) = dx + dy – gcd(dx, dy).The unitSquares(int x1, int y1, int x2, int y2) function takes four values x1, y1 and x2, y2. The absolute difference between the x2 and x1 and the absolute difference ...

Read More

Disarium Number with examples in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 1K+ Views

A number whose sum of its digits powered with its respective position equals to the number itself is called a disarium number.The noOfDigits(int num) function takes the number and return the number of digits by constantly dividing the number by 10 while there is only ones place left. On each iteration the digits variable is incremented to keep the digits track and is returned once the while loop ends.int noOfDigits(int num){    int digits = 0;    int temp = num;    while (temp){       temp= temp/10;       digits++;    }    return digits; }Next, isDisarium(int ...

Read More

Depth of the deepest odd level node in Binary Tree in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 161 Views

Let us first define the struct that would represent a tree node that contains the int key 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 key) function that takes an int key value and assign it to the key member of the node. The function returns the pointer to the created struct Node. Also, the left and right child of the newly created node are set to null.Node* ...

Read More

Deletion in a Binary Tree in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 384 Views

The deletion is to be performed by replacing the deleted mode by bottom and rightmost node.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 newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also, the ...

Read More

Demlo number (Square of 11...1)” in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 260 Views

Demlo numbers are palindromic numbers that are generated by the square of number of form 11..1 given that the number is less than 10 digits.Let us first declare the string variables −string demNum = "1111"; string square = "";Now, we loop till the length of the demNum string. Inside the loop we convert the index value i to string and append it to square variable.for(int i=1 ;i= 1; i--)    square += char(i + '0');ExampleLet us see the following implementation to get a better understanding of demlo numbers − Live Demo#include using namespace std; int main(){    string demNum = ...

Read More

Deleting a binary tree using the delete keyword in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 225 Views

Let us first define our binary tree using a class containing int data, btree_node * rightChild, btree_node * leftChild. The leftChild and rightChild are pointers to btree_node. All the members in our class are public.class btree_node {    public:       int data;       btree_node* leftChild;       btree_node* rightChild;For creating a new node we have the constructor function that takes the int value as parameter to assign it to the newly created node value. The leftChild and rightChild are set to null.btree_node(int data){    this->data = data;    this->leftChild = NULL;    this-> = NULL; ...

Read More

Delete leaf nodes with value k in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 176 Views

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 newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also, the left and right child of the newly created node are set to null.struct Node* newNode(int ...

Read More

Delete leaf nodes with value as x in C++?

AmitDiwan
AmitDiwan
Updated on 16-Jan-2021 254 Views

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 newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also the left and right child of the newly created node are set to null.struct Node* newNode(int ...

Read More
Showing 35151–35160 of 61,248 articles
Advertisements