Programming Articles - Page 1926 of 3366

IPO in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:30:11

439 Views

Suppose one company A wants to start its IPO soon. In order to sell a good price of its shares to B, A would like to work on some projects to increase its capital before the IPO. A has limited resources, it can only finish at most k distinct projects before the IPO. Can you help A, by designing the best way to maximize its total capital after finishing at most k distinct projects?Suppose we have several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding ... Read More

Reverse Pairs in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:29:05

912 Views

Suppose we have an array, in this array we will say one pair (A[i] and A[j]) as important reverse pairs if this satisfies the following condition −if i < j and A[i] > 2* nums[j]We have to find the number of important reverse pairs. So if the input is like [2,8,7,7,2], then the result will be 3.To solve this, we will follow these steps −ans := 0Define a function merge(), this will take an array a, low, mid, high,k := high - low + 1Define an array temp of size ki := low, j = mid + 1, k := 0first := mid + 1while i

Zuma Game in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:26:46

867 Views

Let us consider about the Zuma Game. Suppose we have a row of balls on the table, these balls are colored as red(R), yellow(Y), blue(B), green(G), and white(W). We also have several balls with us.Now, each time, we may choose a ball from our side, and insert it into the row. Then, if there is a group of 3 or more balls in the same color touching, remove them. Keep doing this until no more balls can be removed.We have to find the minimal balls we have to insert to remove all the balls on the table. If we cannot ... Read More

Smallest Good Base in Python

Arnab Chakraborty
Updated on 01-Jun-2020 11:23:44

361 Views

Suppose we have an integer n, we call k >= 2 as a good base of n, when all digits of n base k are 1. So if the number n is given as string, we have to return smallest good base of n as string. So if the number is say 121, then the answer will be 3, as 121 in base 3 is 11111.To solve this, we will follow these steps −Define a method called getSum(), this will take x and lengthset mainSum := 0 and temp := 1for i in range 0 to length – 1 −mainSum ... Read More

Sliding Window Median in C++

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

1K+ Views

Suppose we have a list of numbers, and we have one window size k, we have to find the list of medians using sliding window manner. So, if the distribution is like below −Window PositionMedian13-1-35368113-1-35368-113-1-35368-113-1-35368313-1-35368513-1-353686Here we have considered the k is 3, and the result will be [1, -1, -1, 3, 5, 6]To solve this, we will follow these steps −Define one set arrDefine a function insert(), this will take x, insert x into arrDefine a function delete_(), this will take x, delete x from arr, if this existsDefine a function getMedian()n := size of arra := jump to n/2 ... Read More

Largest Palindrome Product in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:10:12

158 Views

Suppose we have input n, we have to find the largest palindrome that can be made using multiplication of two n digit numbers. As the numbers are very large, we can perform mod using 1337. So if the input is say 2, then the answer will be 987, 987 = (99*91) mod 1337 = 9009 mod 1337 = 987.To solve this, we will follow these steps −maxVal := 10^n – 1minVal := maxVal / 10for initialize h := maxVal, when h > minVal, update (decrease h by 1), do −left := h, right := 0for initialize i := h, when ... Read More

Concatenated Words in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:07:49

210 Views

Suppose we have a list of words. These words are distinct. We have to devise an algorithm that will find all concatenated words in the give list of words. A concatenated word is actually a string that is comprised entirely of at least two shorter words in the given array.So if the words are like ["cow", "cows", "cowsgoatcows", "goat", "goatcowsgoat", "hippopotamuses", "deer", "deercowgoatcow"], then the output will be ["cowsgoatcows", "goatcowsgoat", "deercowgoatcow"]To solve this, we will follow these steps −Define a function isPresent(), this will take str, head, idx, an array dp, if idx >= size of str, then −return trueif ... Read More

Poor Pigs in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:03:44

212 Views

Suppose there are 1000 buckets, one of them is poisonous, others are filled with water. They all look similar. If a pig drinks the poison it will die within 15 minutes. What will be the minimum amount of pigs that we need to find out the poisonous bucket within one hour?So now consider for the general case and devise an algorithm for this. So, the general case is that If there are n different buckets and a pig drinking poison will die within m minutes, how many pigs are needed to find poisonous bucket within p minutes? There is exactly ... Read More

Arithmetic Slices II - Subsequence in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:02:56

253 Views

Suppose we have an array A, where N numbers are present. A subsequence slice of that array is any sequence of integers like (K0, K1, K2, … Kn) such that 0 = 2. So we have to return the number of arithmetic slices.So if the input is like [2, 4, 6, 8, 10], then the answer will be 7, as there are 7 arithmetic slices. [2, 4, 6], [2, 4, 10], [4, 6, 8], [6, 8, 10], [2, 4, 6, 8], [4, 6, 8, 10], [2, 4, 6, 8, 10], To solve this, we will follow these steps −ret := ... Read More

K-th Smallest in Lexicographical Order in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:55:27

792 Views

Suppose we have two values n and k. We have to find the lexicographically kth smallest integer in the range of 1 to n. So if the input is like n = 14 and k = 3, then the output will be 11, as the sequence will be [1, 10, 11, 12, 13, 14, 2, 3, 4, 5, 6, 7, 8, 9], then the kth number is 11.To solve this, we will follow these steps −Define a function findKthNumber(), this will take n, k,curr := 1(decrease k by 1)while k is non-zero, do −steps := call the function calcSteps(n, curr, curr + 1)if steps

Advertisements