Server Side Programming Articles - Page 1751 of 2650

Minimum Distance to Type a Word Using Two Fingers in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:25:38

286 Views

Suppose we have a keyboard layout like below −ABCDEFGHIJKLMNOPQRSTUVWXYZWhere each English uppercase letter is located at some coordinate, as an example, the letter A is placed at (0, 0), the letter B is placed at (0, 1), the letter P is placed at (2, 3) and the letter Z is placed at (4, 1). Now if we have a word, we have to find the minimum total distance to type such string using only two fingers. The distance between two places (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|. And we can start from any ... Read More

Distinct Echo Substrings in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:21:01

178 Views

Suppose we have a string S; we have to find the number of distinct non-empty substrings of S that can be written as the concatenation of some string with itself.So, if the input is like "elloelloello", then the output will be 5, as there are some substrings like "ello", "lloe", "loel", "oell".To solve this, we will follow these steps −prime := 31m := 1^9 + 7Define a function fastPow(), this will take base, power, res := 1while power > 0, do −if power & 1 is non-zero, then −res := res * baseres := res mod mbase := base * ... Read More

Verbal Arithmetic Puzzle in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:17:12

504 Views

Suppose we have an equation, expressions are represented by words on left side and the result on right side. We have to check whether the equation is solvable under the following rules or not −Each character is decoded as one digit (0 to 9).Every pair of different characters must map to different digits.Each words[i] and result are decoded as a number where no leading zeros are present.Sum of numbers on left side will equal to the number on right side.We will check whether the equation is solvable or not.So, if the input is like words = ["SEND", "MORE"], result = ... Read More

Number of Paths with Max Score in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:13:26

202 Views

Suppose we have a square board of characters. We can move on the board starting at the bottom right square marked with the character 'S'. Now we need to reach the top left square marked with the character 'E'. The other squares are labeled either with a numeric character from 1 to 9 or with an obstacle 'X'. In one move we can go up, left or up-left only when there is no obstacle there.We have to find the list of two numbers: the first number is the maximum sum of the numeric characters we can collect, and the second ... Read More

Maximum Candies You Can Get from Boxes in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:09:37

410 Views

Suppose we have n boxes, here each box is given in the format like [status, candies, keys, containedBoxes] there are some constraints −status[i]: an is 1 when box[i] is open and 0 when box[i] is closed.candies[i]: is the number of candies in box[i].keys[i]: is an array contains the indices of the boxes we can open with the key in box[i].containedBoxes[i]: an array contains the indices of the boxes found in box[i].We will start with some boxes given in initialBoxes array. We can take all the candies in any open box and we can use the keys in it to open ... Read More

Shortest Path in a Grid with Obstacles Elimination in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:07:14

611 Views

Suppose we have a m x n grid, here each cell is either 0 or 1. 0 cell is empty and 1 is blocked. In one step, we can move up, down, left or right from and to an empty cell. We have to find the minimum number of steps to walk from the upper left corner cell (0, 0) to the lower right corner cell (m-1, n-1) given that we can eliminate at most k obstacles. If there is no such way, then return -1.So, if the input is like000110000011000and k is 1, then the output will be 6, ... Read More

Minimum Falling Path Sum II in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:04:09

313 Views

Suppose we have a grid arr, this is a square grid, a falling path with non-zero shifts is a choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are present in the same column. We have to find the minimum sum of a falling path with nonzero shifts.So, if the input is like arr is like [[1, 2, 3], [4, 5, 6], [7, 8, 9]], then the output will be 13, as there are different falling paths, these are like [1, 5, 9], [1, 5, 7], [1, 6, 7], [1, ... Read More

Number of Ways to Stay in the Same Place After Some Steps in C++

Arnab Chakraborty
Updated on 08-Jun-2020 11:00:39

285 Views

Suppose there is an array of size arrLen, and we also have a pointer at index 0 in that array. At each step, we can move 1 position to the left, 1 position to the right in the array or stay in the same place.Now suppose we have two integers steps and arrLen, we have to find the number of ways such that the pointer still at index 0 after exactly steps. If the answer is very large then return it modulo 10^9 + 7.So, if the input is like steps = 3, arrLen = 2, then the output will ... Read More

Number of Valid Words for Each Puzzle in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:58:15

229 Views

Suppose there is a puzzle string, a word is valid if both the following conditions valid −word contains the first letter of puzzle.For each letter in word, that letter is in puzzle.Suppose if we consider an example that, if the puzzle is like "abcdefg", then valid words are "face", "cabbage" etc; but some invalid words are "beefed" as there is no "a" and "based" as there is "s" which is not present in the puzzle.We have to find the list of answers, where answer[i] is the number of words in the given word list words that are valid with respect ... Read More

Recover a Tree From Preorder Traversal in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:55:14

525 Views

Suppose there is a binary tree. We will run a preorder depth first search on the root of a binary tree.At each node in this traversal, the output will be D number of dashes (Here D is the depth of this node), after that we display the value of this node. As we know if the depth of a node is D, the depth of its immediate child is D+1 and the depth of the root node is 0.Another thing we have to keep in mind that if a node has only one child, that child is guaranteed to be ... Read More

Advertisements