Longest Arithmetic Subsequence of Given Difference in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:49:02

475 Views

Suppose we have an integer array arr and an integer difference, we have to find the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence is same as the difference. So if the input is like [1, 5, 7, 8, 5, 3, 4, 2, 1] and difference is -2, then the output will be − 4, as the longest arithmetic sequence is [7, 5, 3, 1]To solve this, we will follow these steps −Define a map mn := size of array arr, set ans := 0for i ... Read More

Two Sum BSTs in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:45:28

240 Views

Suppose we have two binary search trees, we have to return True iff there is a node in the first tree and a node in the second tree and sum of these nodes is a given integer target. So if the tree is like −and target is 5, then the result is true.To solve this, we will follow these steps −Define a map sdefine a method called check(), this will take node, target and nodeNumber, this will work as follows −if node is valid, then return falsecurr := value of node, req := target – currif req is present in ... Read More

Maximum Subarray Sum with One Deletion in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:36:31

384 Views

Suppose we have an array of integers; we have to find the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, we can say that we want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible. We have to keep in mind that the subarray needs to be non-empty after deleting one element. So if the input is like [1, -2, 0, 3], then the output will be 4. So ... Read More

Make Palindrome from Substring in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:32:37

205 Views

Suppose we have a string s, we have to make queries on substrings of s. For each query queries[i], there are three parts [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter. If the substring is possible to be a palindrome after the operations mentioned above, the result of the query is true. Otherwise false. We have to find an array answer[], where answer[i] is the result of the i-th query queries[i].For example, if the input is “abcda”, queries is like [[3, 3, ... Read More

Minimum Cost to Connect Sticks in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:27:42

425 Views

Suppose we have some sticks with positive integer lengths. We can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. This will be performed until there is one stick remaining. We have to find the minimum cost of connecting all the given sticks into one stick in this way. So if the stack is [2, 4, 3], then the output will be 14.To solve this, we will follow these steps −Define a max heap priority queue pqinsert all elements of s into pqans := 0while pq has more than ... Read More

Design File System in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:22:05

2K+ Views

Suppose we have to design a file system which provides these two functions −createPath(path, value) − This creates a new path and associates a value to it if possible and returns True. It returns False if the path already exists or its parent path doesn't exist.get(path) − This finds the value associated with a path or returns -1 if the path doesn't exist.The format of a path is one or more concatenated strings of the form − (forward slash) / followed by one or more lowercase English letters. For example, /programming and /programming/problems are valid paths while an empty string ... Read More

Maximum Level Sum of a Binary Tree in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:14:36

313 Views

Suppose we have the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. We have to return the smallest level X such that the sum of all the values of nodes at level X is maximal. So if the tree is like −Then the output will be 2, The level 1 sum = 1, level 2 sum is 7 + 0 = 7, level 2 sum is 7 + (-8) = -1, so max is of level 2, so output is 2.To solve this, we will follow ... Read More

Construct Binary Tree from Preorder and Postorder Traversal in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:14:19

540 Views

Suppose we have two traversal sequences Preorder and Postorder, we have to generate the binary tree from these two sequences. So if the sequences are [1, 2, 4, 5, 3, 6, 7], [4, 5, 2, 6, 7, 3, 1], then the output will beTo solve this, we will follow these steps −ans := make a tree node by taking value pre[0], stack := empty stack, and insert ansi := 1 and j := 0while i < length of pre and j < length of postif stack top value = post[j], then increase j by 1, pop from stack, and go ... Read More

Decoded String at Index in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:09:03

231 Views

Suppose one encoded string S is given. We have to find and write the decoded string to a tape, here the encoded string is read one character at a time and the following steps are performed −If the character read is a letter, that letter is simply written onto the tape.If the character read is a digit, the entire current tape is repeatedly written digit – 1 more times in total.Now if some encoded string S, and an index K is given, find and return the K-th letter (starting indices from 1) in the decoded string.So if the string is ... Read More

Number of Dice Rolls with Target Sum in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:06:31

884 Views

Suppose we have d dice, and each die has f number of faces numbered 1, 2, ..., f. We have to find the number of possible ways (out of fd total ways) modulo 10^9 + 7 to roll the dice so the sum of the face up numbers equal to the target. So if the input is like d = 2, f = 6, target = 7, then the output will be 6. So if we throw each dice with 6 faces, then there are 6 ways to get sum 6, as 1 + 6, 2 + 5, 3 + ... Read More

Advertisements