Programming Articles - Page 1980 of 3366

Clumsy Factorial in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:49:18

2K+ Views

As we know that the factorial of a positive integer n is the product of all positive integers less than or equal to n. So factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We will try to find a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.The clumsy factorial is like clumsy(10) = 10 * 9 / 8 + 7 - 6 * ... Read More

Max Consecutive Ones III in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:45:44

587 Views

Suppose we have an array A of 0s and 1s, we can update up to K values from 0 to 1. We have to find the length of the longest (contiguous) subarray that contains only 1s. So if A = [1,1,1,0,0,0,1,1,1,1,0] and k = 2, then the output will be 6, So if we flip 2 0s, the array can be of like [1,1,1,0,0,1,1,1,1,1,1], the length of longest sequence of 1s is 6.To solve this, we will follow these steps −set ans := 0, j := 0 and n := size of arrayfor i in range 0 to n – 1if A[i] is 0, then decrease k by 1while j

Subarray Sums Divisible by K in C++

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

524 Views

Suppose we have an array A of integers. We have to find the number of contiguous non-empty subarray, that have a sum divisible by k. If A = [4, 5, 0, -2, -3, 1] and k = 5, then the output will be 7. There are seven subarrays. [[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]]To solve this, we will follow these steps −make one map m and set m[0] as 1temp := 0, ans := 0, and n := size of array afor i in range 0 to ... Read More

Minimum Falling Path Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:40:40

294 Views

Suppose we have a square array of integers A, we want the minimum sum of a falling path through A. Falling path is basically a path that starts at any element in the first row, and chooses one element from each row. And the next row's element must be in a column that is different from the previous row's column by at most one. So if the matrix is like −123456789Then the output is 12. There are few different falling paths. these are [1, 4, 7], [1, 4, 8], [1, 5, 7], [1, 5, 8], [1, 5, 9], [2, 4, ... Read More

Minimum Add to Make Parentheses Valid in Python

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

412 Views

Suppose we have a string S of '(' and ')' parentheses, we add the minimum number of parentheses at any positions, so that the resulting parentheses string is valid. A parentheses string is valid if and only if −It is the empty stringIt can be written as XY (X concatenated with Y), where X and Y are valid stringsIt can be written as (A), where A is a valid string.So if the string is like "()))((", then we need to add 4 more parentheses to make the string valid.To solve this, we will follow these steps −if S is empty, ... Read More

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

214 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

206 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

515 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

215 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

Advertisements