Found 7347 Articles for C++

Delete leaf nodes with value k in C++ program

Hafeezul Kareem
Updated on 27-Jan-2021 12:05:33

74 Views

In this tutorial, we are going to learn how to delete the leaf nodes from a tree with the given value.Let's see the steps to solve the problem.Write a struct Node for a binary tree.Write a function to traverse (inorder, preorder, postorder) through the tree and print all data.Initialize the tree by creating nodes with the struct.Initialize the x value.Write a function to delete the leaf nodes with the given value. It accepts two arguments root node and k value.If the root is a null return.Replace the left node of the root with a new root after deletion.Same with the ... Read More

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

Hafeezul Kareem
Updated on 27-Jan-2021 12:02:19

955 Views

In this tutorial, we are going to learn how to delete the leaf nodes from a tree with the given value.Let's see the steps to solve the problem.Write a struct Node for a binary tree.Write a function to traverse (inorder, preorder, postorder) through the tree and print all data.Initialize the tree by creating nodes with the struct.Initialize the x value.Write a function to delete the leaf nodes with the given value. It accepts two arguments root node and x value.If the root is a null return.Replace the left node of the root with a new root after deletion.Same with the ... Read More

Delete array element in given index range [L – R] in C++ Program

Hafeezul Kareem
Updated on 27-Jan-2021 12:01:58

20K+ Views

In this tutorial, we are going to learn how to delete elements from the given range. Let's see the steps to solve the problem.Initialize the array and range to delete the elements from.Initialize a new index variable.Iterate over the array.If the current index is not in the given range, then update the element in the array with a new index variableIncrement the new index.Return the new index.ExampleLet's see the code. Live Demo#include using namespace std; int deleteElementsInRange(int arr[], int n, int l, int r) {    int i, newIndex = 0;    for (i = 0; i < n; i++) ... Read More

Delete an element from array using two traversals and one traversal in C++ program

Hafeezul Kareem
Updated on 27-Jan-2021 12:01:37

340 Views

In this tutorial, we are going to learn how to delete an element with two loops and on loop. We don't need to delete the element. We will just replace the deleting element with the next elements.Two TraversalsLet's see the steps to delete an element from the array using two loops.Initialize the array and delete the element.Write a function to delete the element.Iterate over the array and search for the element.If the element found, break the loop.If the element is found, decrease the size of the array.Move all the elements to their previous index.Return the new size of the array.ExampleLet's ... Read More

Find Maximum number possible by doing at-most K swaps in C++

sudhir sharma
Updated on 27-Jan-2021 05:08:36

532 Views

In this problem, we are given two integer values n and k. Our task is to find the Maximum number possible by doing at-most K swaps. Problem description: Here, we need to calculate the number which is maximum and created after swapping at-most k digits of the number.Let’s take an example to understand the problem,  Input: n = 538 k = 1Output: 835Explanation: We will swap 8 and 5.Solution ApproachTo solve the problem, we need to swap digits of the number k times and check if the number from is maximum.  We need to find the maximum digit of the number and then swap the ... Read More

Find maximum level sum in Binary Tree in C++

sudhir sharma
Updated on 27-Jan-2021 05:08:07

106 Views

In this problem, we are given  a binary Tree with positive and negative values. Our task is to Find maximum level sum in Binary Tree. Problem Description: We have a binary tree, we will find the sum of all levels in the binary tree and then return the maximum of them.Let’s take an example to understand the problem, Input: Output: 5Explanation: Sum of elements at level 1: 3Sum of elements at level 2: -3 + 4 = 1Sum of elements at level 3: 5 - 1 + 6 - 5 = 5Solution ApproachTo solve the problem, we need to traverse the tree using the level ... Read More

Prime Factorization using Sieve O(log n) for multiple queries in C++

sudhir sharma
Updated on 27-Jan-2021 05:07:35

2K+ Views

In this problem, we need to create a program to calculate Prime Factorization using Sieve O(log n) for multiple queries. As the general method takes O(sqrt(n) ) time which will increase the time required to a huge extent from multiple queries.Let’s recap first, Prime factorization of a number includes ONLY the prime factors, not any products of those prime factors.Sieve of Eratosthenes is an algorithm to generate all prime numbers within the given range.Solution ApproachThe solution to the problem is found by finding the smallest factor that divides the number, saving it as a factor and updating the number by dividing ... Read More

Principles of Risk Management and Paradigm in C++

sudhir sharma
Updated on 27-Jan-2021 05:03:15

600 Views

Risk Management is the approach that is used to manage all available resources and make the best use of the resources available in the system.The project manager will analyse risks from all categories and there might be some risks in the working of the project in the runtime environment.Principles of Risk ManagementThere are 5 basic principles of Risk management. They are:GLOBAL PERSPECTIVE: This risk analysis of software in context of system and business problems planned to be solved. This analysis takes underconsideration the larger system definition, design and implementation.FORWARD LOOKING VIEW: This analysis considers all possible solutions to future risks to the product. Here, ... Read More

Print a number as string of 'A' and 'B' in lexicographic order in C++

sudhir sharma
Updated on 27-Jan-2021 05:00:50

76 Views

In this problem, we are given a number N. Our task is to create a program to Print a number as string of ’A’ and ‘B’ in lexicographic order. Representation of all numbers as string of ‘A’ and ‘B’ is1 = A 2 = B 3 = AA 4 = AB 5 = BA 6 = BB 7 = AAA 8 = AABLet’s take an example to understand the problem,  Input: N = 12Output: BABSolution ApproachThe string of ‘A’ and ‘B’ is similar to a binary number. To find the string, we will first find the length of string, using the fact that there are 2 numbers of length 1 ... Read More

Print all longest common sub-sequences in lexicographical order in C++

sudhir sharma
Updated on 27-Jan-2021 05:00:17

602 Views

In this problem, we are given two string str1 and str2. Our task is to create a program to Print all longest common subsequences in lexicographical order. Let’s take an example to understand the problem,  Input: str1 = “gfare” ,  str2 = “rfare”Output: fareSolution ApproachIn this problem, we will find all possible longest common subsequences and store them in a 2D matrix using Dynamic programming. After this, we will print sorted output by searching characters from a to z in the LCS.Program to illustrate the working of our solution, ExampleLive Demo#include #include #define MAX 100 using namespace std; int LCSLength = 0; ... Read More

Advertisements