C++ Articles

Page 69 of 597

Diagonal Sum of a Binary Tree in C++?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 267 Views

To consider the nodes that are passing between lines of slope -1. The diagonal sum of the binary tree will be calculated by the sum of all nodes data that are present between these lines of reference.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 ...

Read More

Diagonally Dominant Matrix in C++?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 464 Views

A matrix is said to be diagonally dominant matrix if for every matrix row, the diagonal entry magnitude of the row is larger than or equal to the sum of the magnitudes of every other non-diagonal entry in that row.Let us first define a constant int variable N with value 3 which represents our matrix dimensions.const int N = 3;The isDDM(int mat[N][N], int n) is a Boolean function that takes a copy of our matrix and the size of our matrix. Inside we iterate the rows and columns of our matrix using nested for loop. We then find the sum ...

Read More

Check if it is possible to return to the starting position after moving in the given directions in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 262 Views

Suppose we are at position (0, 0) We have a string represents consecutive directions using four letters. We have to check whether we can return at (0, 0) position after considering all of the given directions. The symbols areE for eastW for westN for northS for south.So, if the input is like "EENWWS", then the output will be true, move east two units, then go north, then west two units then again south, so this is the starting position.To solve this, we will follow these steps −l := size of moves arrayif l is same as 0, then −return truelft ...

Read More

Elements of an array that are not divisible by any element of another array in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 460 Views

In this problem, we are given two arrays arr1[] and arr2[]. Our task is to create a program to find the elements of an array that are not divisible by any element of another array. Problem Description: Here, we need to find all elements from arr1 that are not divisible by any elements of arr2.Let’s take an example to understand the problem, Input: arr1[] = {17, 15, 5, 12, 8}        arr2[] = {5, 4}Output: 17Explanation −Elements of arr1 and elements dividing them, 17 -> no element can divide it.15 -> 5 divides the element.5 -> 5 divides the element.12 -> 4 ...

Read More

Elo Rating Algorithm in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 702 Views

Elo Rating Algorithm is a rating algorithm used to rank players in competitive games. The ranking of player of the competition is based on ranting which changes based on the performance of the player as follows, For a game between two players of different ratings. Let’s say there are two players competing against each other−Player1                          Player2Rating of player1 is greater than player2.If player1 wins the game some player will be transferred from player1 to player2 and vice versa if player2 wins.But the amount of rating to be transferred for victory ...

Read More

Enumeration of Binary Trees in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 591 Views

Enumeration of Binary Tree is counting the total number of distinct unlabeled binary trees of a given size (specific number of nodes). In this article, we will create a program to count the number of Binary Trees of n nodes.Based on labeling of nodes of binary tree, it is of two types:Labeled Binary TreeUnlabeled Binary TreeLabeled Binary Tree: It is a binary Tree in which the nodes of a tree are labeled with values.Different Type of Labeled Binary Tree for a given number of nodes :Number of nodes N = 2Similarly, We can find the number of distinct labeled binary Tree for ...

Read More

Equidigital Numbers in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 197 Views

Equidigital numbers are mathematically special numbers in which the number of digits in the number is equal to the number in its prime factorization.In this problem, we are given an integer value n. Our task is to create a program to all equidigital numbers up to n.Let’s take an example to understand the problem, Input: n =  12Output:  1 2 3 5 7 10 11Solution Approach:A simple solution to the problem would be finding the factors of the number and check if the number of primes is equal to the number of digits in the number .The prime factors can be ...

Read More

Evaluation of Prefix Expressions in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 16K+ Views

In this article, we will discuss the evaluation of prefix Expression. Prefix ExpressionIn this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.For more read.Example:* + 6 9 - 3 1Prefix expressions are evaluated faster than infix expressions. Also, there are no brackets in prefix expressions which make it evaluate quicker.Algorithm to evaluate Prefix Expression:The evaluation of prefix expression requires a stack data structure. We will push the operators in the stack and then solve the ...

Read More

Evaluation of Expression Tree in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 1K+ Views

In this problem, we are given an expression tree that consist of binary operations like +, - , /, *. We need to do the evaluation of the expression tree and then return the result.Expression Tree is a special type of binary tree in which each node either consist of an operator or operand which are distributed as−Leaf nodes of the tree are values on which the operation is to be performed. Non-leaf nodes consist of the binary operator denoting the operation to be performed. Let’s take an example to understand the problem, Input: Output: 1Explanation: Decoding the expression tree, Exp           ...

Read More

Extended Midy's theorem in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 201 Views

Midy’s Theorem is a statement used for decimal expansion of numbers denoted by n/p, where n is any number and p is a prime number and a/p has a repeating decimal with even period.In Extended Midy’s Theorem, the repeating portion is divided into m digits, then their sum is a multiple of 10m - 1. Program to illustrate Extended Midy’s Theorem: Example#include using namespace std; string findDecimalValue(int num, int den) {        string res;    unordered_map mp;    int rem = num % den;    while ((rem != 0) && (mp.find(rem) == mp.end())) {     ...

Read More
Showing 681–690 of 5,962 articles
« Prev 1 67 68 69 70 71 597 Next »
Advertisements