Found 7197 Articles for C++

Minimum Factorization in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:26:26

138 Views

Suppose we have a positive integer x, we have to find the smallest positive integer b whose multiplication of each digit equals to x. If we have no such answer then return 0.So, if the input is like 48, then the output will be 68To solve this, we will follow these steps −ret := 0, mul := 1if a < 2, then:return afor initialize i := 9, when i >= 2, update (decrease i by 1), do −while a mod i is same as 0, do −ret := i * mul + retmul := mul * 10a := a / ... Read More

Add Bold Tag in String in C++

Nishu Kumari
Updated on 25-Jul-2025 13:00:47

3K+ Views

 A string is a sequence of characters, like words, numbers, or sentences, enclosed in double quotes ("). The given task is to add a closed pair of HTML bold tags around a string using C++.Let us understand this with an example: Input: abc Output: abc Add Bold Tag in String in C++ To add bold tags to a string in C++, we use string concatenation. This is the process of joining two or more strings together to form a new string. In C++, this can be done using the '+' operator. To wrap a string with bold ... Read More

Kill Process in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:21:40

2K+ Views

Suppose we have n processes, here each process has a unique id called PID or process id and its PPID (parent process id) is also there.Each process only has one parent process, but may have one or more child processes.This is just like a tree structure. Only one process has the PPID = 0, which means this process has no parent process. All the PIDs will be unique positive integers.We will use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID. So, ... Read More

Squirrel Simulation in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:16:20

348 Views

There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Your goal is to find the minimal distance for the squirrel to collect all the nuts and put them under the tree one by one. The squirrel can only take at most one nut at one time and can move in four directions - up, down, left, and right, to the adjacent cell. The distance is represented by the number of moves.So, if the input is like Height: 5 Width: 7 Tree position: [2, 2] Squirrel: [4, 4] Nuts: [[3, 0], [2, ... Read More

Longest Line of Consecutive One in Matrix in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:14:24

364 Views

Suppose we have one binary matrix M, we have to find the longest line of consecutive one in that matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.So, if the input is like011001100001then the output will be 3To solve this, we will follow these steps −ret := 0n := row of Mm := column of MDefine one 3D array dp of order n x m x 4for initialize i := 0, when i < m, update (increase i by 1), do −for initialize j := 0, when j < 4, update (increase j by 1), do −dp[0, i, j] ... Read More

Split Concatenated Strings in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:11:41

565 Views

Suppose we have a list of strings, we can concatenate these strings together into a loop, where for each string we can choose to reverse it or not. Among all of the possible loops, we need to find the lexicographically largest string after cutting the loop, which will make the looped string into a regular one. Specifically, to find the lexicographically largest string, we need to experience two phases −Concatenate all the strings into one loop, where we can reverse some strings or not and connect them in the same order as given.Cut and make one cutting point in any ... Read More

Binary Tree Longest Consecutive Sequence II in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:08:24

296 Views

Suppose we have a binary tree; we have to find the length of the Longest Consecutive Path in that Binary Tree. Here the path can be either increasing or decreasing. So as an example [1, 2, 3, 4] and [4, 3, 2, 1] are both considered as a valid path, but the path [1, 2, 4, 3] is not a valid one.Otherwise, the path can be in the child-Parent-child sequence, where not necessarily be parent-child order.So, if the input is likethen the output will be 3 as the longest consecutive path will be like [1, 2, 3] or [3, 2, ... Read More

Split Array with Equal Sum in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:05:16

864 Views

Suppose we have an array with n integers, we have to find if there are triplets (i, j, k) which follows these conditions −0 < i, i + 1 < j, j + 1 < k < n - 1Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) will be same.The subarray (L, R) is a slice of the original array starting from the element indexed L to the element indexed R.So, if the input is like [1, 2, 1, 2, 1, 2, ... Read More

Boundary of Binary Tree in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:02:36

223 Views

Suppose we have a binary tree, we have to find the values of its boundary in anti-clockwise direction starting from root. Here boundary includes left boundary, leaves, and the right boundary in order without duplicate nodes.The left boundary is the path from root to the left-most node.The Right boundary is the path from root to the right-most node.When the root doesn't have left subtree or right subtree, then the root itself is left boundary or right boundary.So, if the input is likethen the output will be [1, 2, 4, 7, 8, 9, 10, 6, 3]To solve this, we will follow ... Read More

Output Contest Matches in C++

Arnab Chakraborty
Updated on 16-Nov-2020 13:58:18

206 Views

Suppose we have n teams and we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the team with rank n, this strategy is to make the contest more interesting. Now we have to find their final contest matches in the form of a string.These teams are given in the form of positive integers from 1 to n, which represents their initial rank. So, rank 1 is the strongest team, and Rank n is the weakest team. We will use parentheses and commas to represent the contest team ... Read More

Advertisements