Found 7347 Articles for C++

Program to count number of valid triangle triplets in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:16:17

644 Views

Suppose we have an array of numbers, we have to find the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. So if the input is like [2, 2, 3, 4], then the result will be 3 as there are three triplets [2, 3, 4] using first 2, [2, 3, 4] using second 2, and [2, 2, 3].To solve this, we will follow these steps −ret := 0, n := size of nums, sort numsfor i in range n − 1 down to 0right := i − 1, ... Read More

Program to pruning a given binary tree in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:10:20

241 Views

Suppose we have a binary tree, where every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null and node value is 0, then ... Read More

Program to find spreadsheet column number from the column title in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:02:35

107 Views

Suppose we have a column title of spreadsheet. We know that the spreadsheet column numbers are alphabetic. It starts from A, and after Z, it will AA, AB, to ZZ, then again AAA, AAB, to ZZZ and so on. So column 1 is A, column 27 is Z. Here we will see how to get the column letter if number of column is given. So if the column number is 80, then it will be CB. So we have to find the corresponding column title from the number. If the input is like 30, it will AD.Example Live Demo#include #include using ... Read More

Program to find spreadsheet column title from the column number in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:00:42

182 Views

Suppose we have a positive integer value; we have to find its corresponding column title as appear in a spread sheet. So [1 : A], [2 : B], [26 : Z], [27 : AA], [28 : AB] etc.So, if the input is like 29, then the output will be AC.To solve this, we will follow these steps −while n is non-zero, do −n := n − 1res := res + n mod 26 + ASCII of 'A'n := n / 26reverse the array resreturn resLet us see the following implementation to get better understanding −Example Live Demo#include using namespace std; ... Read More

Program to find sum of the deepest nodes in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:36:28

75 Views

Suppose we have a binary tree; we have to find the sum of values of its deepest leaves. So if the tree is like −Then the output will be 11.To solve this, we will follow these steps −Define a map m, and maxDepthDefine a recursive method solve(), this will take node and level, initially level is 0if node is not present, then returnmaxDepth := max of level and maxDepthincrease m[level] by value of nodesolve(left of node, level + 1)solve(right of node, level + 1)In the main method, setup maxDepth := 0, then solve(root, 0)return m[maxDepth]Let us see the following implementation ... Read More

Program to find sum of the right leaves of a binary tree in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:33:29

109 Views

Suppose we have a binary tree we have to find the sum of all right leaves in a given binary tree.So, if the input is likethen the output will be 17, as there are two right leaves in the binary tree, with values 7 and 10 respectively.To solve this, we will follow these steps −Define a function dfs(), this will take node, add, if node is null, then −returnif left of node is null and right of node is null and add is non-zero, then −ret := ret + val of nodedfs(left of node, false)dfs(right of node, true)From the main ... Read More

Program to solve partially filled Sudoku Grid in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:26:07

239 Views

Suppose we have a partially filled Sudoku grid and we have to solve this. We know that Sudoku is a 9 × 9 number grid, and the whole grid are also divided into 3 × 3 boxes There are some rules to solve the Sudoku.We have to use digits 1 to 9 for solving this problem.One digit cannot be repeated in one row, one column or in one 3 × 3 box.Using backtracking algorithm, we will try to solve Sudoku problem. When some cell is filled with a digit, it checks whether it is valid or not. When it is ... Read More

Program to check whether a string is subsequence of other in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:17:35

1K+ Views

Suppose we have two strings S and T. We have to check whether S is subsequence of T or not.So, if the input is like S = "abc", T = "adbrcyxd", then the output will be TrueTo solve this, we will follow these steps −if s is same as t, then −return truen := size of s, m := size of tj := 0for initialize i := 0, when i < n, update (increase i by 1), do −if t[j] is same as s[i], then −(increase j by 1)if j is same as size of t, then −return truereturn falseLet ... Read More

Program to multiply two strings and return result as string in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:13:42

1K+ Views

Suppose we have two numbers as string. We have to multiply them and return the result also in string. So if the numbers are “28” and “25”, then the result will be “700”To solve this, we will follow these steps −Taking two arguments x and y it indicates x divides yif x < −Infinity and y = 1, then return infinitya := |x|, b := |y| and ans := 0while a − b >= 0p := 0while a − (left shifted b (left shifted 1 p times)) >= 0p := p + 1a := a − (left shift b, p ... Read More

Program to reverse a sentence words stored as character array in C++

Arnab Chakraborty
Updated on 21-Oct-2020 10:39:52

1K+ Views

Suppose we have one input string sentence where each element is stored as single character, we have to reverse the strings word by word.So, if the input is like ["t", "h", "e", " ", "m", "a", "n", " ", "i", "s", " ", "n", "l", "c", "e"], then the output will be ["n", "l", "c", "e", " ", "i", "s", " ", "m", "a", "n", " ", "t", "h", "e"]To solve this, we will follow these steps −reverse the array sj := 0n := size of sfor initialize i := 0, when i < n, update (increase i by 1), ... Read More

Advertisements