C++ Articles

Page 219 of 597

Type Conversion in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Here we will see what are the type conversion techniques present in C++. There are mainly two types of type conversion. The implicit and explicit.Implicit type conversionThis is also known as automatic type conversion. This is done by the compiler without any external trigger from the user. This is done when one expression has more than one datatype is present.All datatypes are upgraded to the datatype of the large variable.bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long doubleIn the implicit conversion, it may lose ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 374 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#include using namespace ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 171 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

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 383 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#include #include using namespace std; void ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 269 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 follow these steps −Create empty stackMake the first value as root, and push it into stack.Now keep pooping while the stack is not empty and the next value is greater than stack top element, make this as right child of the last popped node. Now push the new node to the stack.When the next value is less than the top element, ...

Read More

Construct BST from its given level order traversal in C++

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

Suppose we have one level order traversal. From this traversal. we have to generate the tree So if the traversal is like [7, 4, 12, 3, 6, 8, 1, 5, 10], then the tree will be like −To solve this, we will use recursive approach. The first element will be the root. The second element will be the left child, and third element will be right child (If the condition of BST satisfies), this property will be satisfied for all elements. So we will follow these steps −At first we have to take the first element of the array, and ...

Read More

Convert a given Binary tree to a tree that holds Logical AND property on C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 192 Views

In this tutorial, we will be discussing a program to convert a given Binary tree to a tree that holds Logical AND property.For this we will be provided with a binary tree. Our task is to convert it into a tree that holds the logical AND property means that a node has a value of the AND operation of its children nodes. Note that every node can have a value either zero or one.Example#include using namespace std; //node structure of binary tree struct Node{    int data;    struct Node* left;    struct Node* right; }; //creation of a new ...

Read More

Convert a given Binary Tree to Doubly Linked List (Set 1) in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 265 Views

In this tutorial, we will be discussing a program to convert a binary tree to a doubly linked list.For this we will be provided with a binary tree. Our task is to convert it into a doubly linked list such that the left and right pointers become the previous and next pointers. Also the sequential order of the doubly linked list must be equal to the inorder traversal of the binary tree.For this we are having a very straight forward approach. We will be traversing the binary tree in in order way making the nodes of the doubly linked list ...

Read More

Convert a given Binary Tree to Doubly Linked List (Set 2) in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 199 Views

In this tutorial, we will be discussing a program to convert a binary tree to a doubly linked list.For this we will be provided with a binary tree. Our task is to convert it into a doubly linked list such that the left and right pointers become the previous and next pointers. Also the sequential order of the doubly linked list must be equal to the inorder traversal of the binary tree.For this we are having a different approach. We will be traversing the binary tree in reverse inorder way. Along with we will be creating new nodes and moving ...

Read More

Convert a normal BST to Balanced BST in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 282 Views

In this tutorial, we will be discussing a program to convert a normal binary search tree to balanced binary search tree.For this we will be provided with a skewed binary search tree either left or right. Our task is to convert it into a balanced binary search tree following a certain set of rules.Example#include using namespace std; //node structure of tree struct Node{    int data;    Node* left, *right; }; //traversing tree and storing node pointers //in vector nodes void store_nodes(Node* root, vector &nodes){    if (root==NULL)       return;    store_nodes(root->left, nodes);    nodes.push_back(root);    store_nodes(root->right, ...

Read More
Showing 2181–2190 of 5,961 articles
« Prev 1 217 218 219 220 221 597 Next »
Advertisements