Found 26504 Articles for Server Side Programming

Fruit Into Baskets in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:35:38

1K+ Views

Suppose we have a row of trees, the i-th tree produces fruit with type tree[i]. we can start at any tree of our choice, then repeatedly perform these steps −Add one piece of fruit from this tree to our baskets. If there is no chance, then stop.Move to the next tree to the right of the current one. If there is no tree to the right, then stop.We have two baskets, and each basket can carry any quantity of fruit, but we want each basket should only carry one type of fruit each. We have to find the total amount ... Read More

Online Stock Span in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:33:05

202 Views

Suppose we have an API, that collects daily price quotes for some stock, and returns the span of that stock's price for the current day. Here the span of the stock's price today is defined as −The maximum number of consecutive days (starting from today and going backwards) where the price of the stock was less than or equal to today's price.For example, if we see 7 days stock share records like [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6]. We have to write the actual module for ... Read More

Find and Replace Pattern in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:29:54

191 Views

Suppose we have a list of words and a pattern, and we have to find which words in words matches the pattern. Here a word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the target word. We have to find a list of the words in words that match the given pattern.So for example, if the input is like ["abc", "deq", "mee", "aqq", "dkd", "ccc"] and pattern is “abb”, then the output will be [“mee”, “aqq”], here mee and aqq are matching the ... Read More

Construct Binary Tree from Preorder and Postorder Traversal in Python

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

483 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

206 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

Most Profit Assigning Work in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:04:56

360 Views

Suppose we have jobs difficulty[i] and this array indicates the difficulty of the ith job, and profit[i] is the profit of the ith job. Now consider we have some workers. worker[i] is the ability of the ith worker, this means that this worker can only complete a job with difficulty at most worker[i]. Every worker can do at most one job, but one job can be completed multiple times. We have to find what is the most profit we can make?For example, if the input is like difficulty = [2, 4, 6, 8, 10] and profit = [10, 20, 30, ... Read More

Number of Matching Subsequences in C++

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

334 Views

Suppose we have a string S and a dictionary of words words, find the number of words[i] that is a subsequence of S. So if the input is S= “abcde” and dictionary is [“a”, “bb”, “acd”, “ace”], then output will be 3. Because there are three sequence of words in the dictionary, that are a subsequence of S: “a” “acd” and “ace”To solve this, we will follow these steps −n := size of words arraycreate one map mfor i in range 0 to size of wordsinsert words[i] into the map m[words[i, 0]] positionans := 0for i in range 0 to ... Read More

Reorganize String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 05:57:20

641 Views

Suppose we have a string S, check whether the letters can be rearranged so that two characters that are adjacent to each other are not the same. If that is possible, output any possible result. If that is not possible, return the empty string. So if the input is like “AAB”, then the output will be “ABA”.To solve this, we will follow these steps −Make a priority queue of integer character pairs called pq, define one map mn := size of the stringstore the character frequency in map mfor each key-value pair p in minsert (integer part of p, character ... Read More

Kth Smallest Element in a Sorted Matrix in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:54:09

451 Views

Suppose we have a n x n matrix where each of the rows and columns are sorted in increasing order, we have to find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth unique element. So if the input is like [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k = 8, then the output will be 13.To solve this, we will follow these steps −define one method called checkVal() and the arguments are matrix and valuei := 0, j := length of matrix[0] – ... Read More

Guess Number Higher or Lower II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:25:59

310 Views

Suppose we are playing the Guess Game. The rules of the game is as follows −Player1 pick a number from 1 to n. player2 have to guess which number is picked by player1.Every time player2 guess wrong, player1 will tell whether the number that is picked is higher or lower.However, when a player guess a particular number x, and another player guess wrong, another player has to pay $x. The game will end, when player2 got the correct answer.For example if n = 10, and the player1 has taken 8In the first round, player2 tells the number is 5, that ... Read More

Advertisements