Found 7347 Articles for C++

Next Closest Time in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:44:10

254 Views

Suppose we have a time represented in the format "HH: MM", we have to generate the next closest time by reusing the current digits. We can use the digit an unlimited number of times.So, if the input is like "19:34", then the output will be "19:39" as the next closest time choosing from digits 1, 9, 3, 4, is 19:39. It is not 19:33, because this occurs 23 hours and 59 minutes later.To solve this, we will follow these steps −Define a function eval(), this will take x, a := convert x[0] to stringa := a + x[1]b := convert ... Read More

Path Sum IV in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:42:17

219 Views

Suppose we have a list of integers that is representing a binary tree with a depth smaller than 5. If the depth of a tree is less than 5, then this tree can be represented by a list of three-digit integers. For each integer in this list −The hundreds digit is representing the depth D of this node, 1

Equal Tree Partition in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:38:49

342 Views

Suppose we have a binary tree with n nodes, our task is to check whether it's possible to partition the tree to two trees which have the equal sum of values after deleting exactly one edge on the original tree.So, if the input is likethen the output will be true.To solve this, we will follow these steps −Define one stack stDefine a function solve(), this will take node, if node is null, then −return 0leftSum := solve(left of node)rightSum := solve(right of node)curr := val + leftSum + rightSum of nodeinsert curr into streturn currFrom the main method do the ... Read More

4 Keys Keyboard in C++

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

789 Views

Suppose we will try to write the letter ‘A’, using the keyboard. Our goal is to use only four keys and try to write maximum ‘A’s on the text field. The keys are ‘A’, ‘C’, ‘V’, and ‘Ctrl’.To write a maximum number of A, we will use Ctrl + A to select All, Ctrl + C to copy, and Ctrl + V to paste.So, if the input is like the number of keystrokes is 7 then the output will be 9 as of press A three times.Then Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+VTo solve this, we will follow these steps −if keyStrokes ... Read More

Find the Derangement of An Array in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:29:44

257 Views

Suppose we have an array consisting of n numbers from 1 to n in increasing order, we have to find the number of derangements it can generate.We know that in combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element will appear in its original position. The answer may be very large, so return the output mod 10^9 + 7.So, if the input is like 3, then the output will be 2, as the original array is [1, 2, 3]. The two derangements are [2, 3, 1] and [3, 1, 2].To solve this, ... Read More

Minimum Factorization in C++

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

97 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++

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

2K+ Views

Suppose we have a string s and a list of strings called dict, we have to add a closed pair of bold tag and to wrap the substrings in s that exist in that dict. When two such substrings overlap, then we have to wrap them together by only one pair of the closed bold tags. Also, if two substrings wrapped by bold tags are consecutive, we need to combine them.So, if the input is like s = "abcxyz123" dict is ["abc", "123"], then the output will be "abcxyz123"To solve this, we will follow these steps −n := ... 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

238 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

293 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

Advertisements