Suppose there is a candy store where N different types of candies are available and the prices of all N different types of candies are given. The store also provides an attractive offer. According to this offer, we can buy a single candy from the store and get maximum of K different types of other candies for free. We have to find the minimum amount of money we have to spend to buy all the N different types of candies. We also have to find maximum amount of money we have to spend to buy all the N different types ... Read More
Suppose we have an array of size n, if the elements in the array, are in range from 0 to k-1. Where k is denoted as a positive integer and k max_val, thenmax_val := A[i]result := ireturn resultExample Let us see the following implementation to get better understanding − Live Demodef get_max_repeating(A, k): n = len(A) for i in range(n): A[A[i]%k] += k max_val = A[0] result = 0 for i in range(1, n): if A[i] > max_val: max_val = A[i] result ... Read More
Suppose we have a given number N that in range (1= j and table[i - j] != -1): table[i] = max(table[i], table[i - j] + 1) return table def max_summ(table, n): if (n < max_val): return table[n] else: t = int((n - max_val) / 4)+ 1 return t + table[n - 4 * t] n = 16 table = pre_calc() print(max_summ(table, n))Input16Output4
Suppose there are n bikes and each can cover 100 km when they are fully fueled. We have to find the maximum amount of distance we can go using these n bikes. Here we can assume that all bikes are similar and a bike consumes 1 litre of fuel to cover 1 km distance. So, if n bikes start from same point and run parallel, we can go only 100 km, in this case our target is to cover maximum distance, with minimum fuel. And minimum wastage of fuel means minimum number of bikes used. If the bikes run serially, ... Read More
Suppose we have an array of pairs A; we have to find the maximum cost for selecting at most K pairs. In this case, the cost of an array of pairs type elements is the product of the sum of first elements of the selected pair and the smallest among the second elements of the selected pairs. As an example, if these pairs are selected (4, 8), (10, 3) and (3, 6), then the cost will be (4+10+3)*(3) = 51, for K=3So, if the input is like A = [(15, 5), (65, 25), (35, 20), (20, 5), (35, 20), (15, ... Read More
Suppose we have a string we have to return the longest possible substring that has exactly k number of unique characters, if there are more than one substring of longest possible length, return any of them.So, if the input is like s = "ppqprqtqtqt", k = 3, then the output will be rqtqtqt as that has length 7.To solve this, we will follow these steps −N := 26Define a function is_ok() . This will take count, kval := 0for i in range 0 to N, doif count[i] > 0, thenval := val + 1return true when (k >= val)From the ... Read More
Suppose we have an array A of n different numbers and another positive integer K, we have to find the longest sub-sequence in the array having Least Common Multiple (LCM) at most K. After than return the LCM and the length of the sub-sequence, following the indexes (starting from 0) of the elements of the obtained sub-sequence. Otherwise, return -1.So, if the input is like A = [3, 4, 5, 6], K = 20, then the output will be LCM = 12, Length = 3, Indexes = [0, 1, 3]To solve this, we will follow these steps −n := size ... Read More
Suppose we have a given string, we have to find the largest sub-string which is a prefix, a suffix and a sub-string of that given string. If there is no such substring, then return -1.So, if the input is like "languagepythonlanguageinterestinglanguage", then the output will be "language"To solve this, we will follow these steps −Define a function get_lps() . This will take stringn := size of stringlong_pref_suff := an array of size n, and fill with 0size := 0, long_pref_suff[0] := 0, i := 1while i < n is non-zero, doif string[i] is same as string[size], thensize := size + ... Read More
Suppose we have an array A of n numbers, where A[i] indicates the number of distinct characters in the prefix of length (i + 1) of a string s, we have to find the lexicographically smallest string that satisfies the given prefix array. All characters will be lowercase English alphabets [a-z]. If there is no such string then return -1.So, if the input is like A = [1, 1, 2, 3, 4], then the output will be aabcd as prefix[0] has 1 distinct character, prefix[1] has 1 distinct character, prefix[2] has 2 distinct characters, prefix[3] has 3 distinct characters, prefix[4] ... Read More
Suppose we have two arrays A and B with n numbers, we have to rearrange the elements of B in itself in a way such that the sequence formed by (A[i] + B[i]) % n after rearranging it is lexicographically smallest. Finally we will return the lexicographically smallest possible sequence.So, if the input is like A = {1, 2, 3, 2}, B = {4, 3, 2, 2}, then the output will be [0, 0, 1, 2]To solve this, we will follow these steps −n := size of aDefine one map my_mapDefine one set my_setfor initialize i := 0, when i ... Read More