Server Side Programming Articles - Page 1626 of 2650

Find the maximum cost of an array of pairs choosing at most K pairs in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:53:46

208 Views

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

Find the longest substring with k unique characters in a given string in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:52:00

656 Views

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

Find the longest subsequence of an array having LCM at most K in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:49:33

501 Views

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

Find the longest sub-string which is prefix, suffix and also present inside the string in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:47:16

1K+ Views

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

Find the lexicographically smallest string which satisfies the given condition in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:44:51

463 Views

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

Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:42:47

453 Views

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

Find the lexicographically largest palindromic Subsequence of a String in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:40:13

340 Views

Suppose we have a string S; we have to find the lexicographically largest palindromic subsequence of that string.So, if the input is like "tutorialspointtutorial", then the output will be "uu"To solve this, we will follow these steps −ans := blank stringmax_val := s[0]for i in range 1 to size of s, domax_val := maximum of max_val, s[i]for i in range 0 to size of s, doif s[i] is same as max_val, thenans := ans + s[i]return ansExample Let us see the following implementation to get better understanding − Live Demodef largest_palindromic_substr(s):    ans = ""    max_val = s[0]    for i ... Read More

Find the largest rectangle of 1’s with swapping of columns allowed in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:34:45

205 Views

Suppose we have a binary matrix, we have to find the largest rectangle of all 1's in that given matrix. The rectangle can be built by swapping or exchanging any pair of columns of that matrix.So, if the input is like100101001111010then the output will be the 6 in this case. The rectangle can be generating by exchanging column 1 with 3. The matrix after exchanging will be −001100011110110To solve this, we will follow these steps −row := size of matcol := size of mat[0]temp := a matrix of order (row + 1) x (col + 1), and fill with 0for ... Read More

Find the largest Perfect Subtree in a given Binary Tree in Python

Arnab Chakraborty
Updated on 20-Aug-2020 07:31:46

784 Views

Suppose we have a given Binary Tree; we have to find the size of largest Perfect sub-tree in that given Binary Tree. As we know the perfect binary tree is a binary tree in which all internal nodes have two children and all leaves are at the identical level.So, if the input is likethen the output will be 3, and the subtree isTo solve this, we will follow these steps −Define one block called RetType, this will hold isPerfect, height and rootTree, they are all initially 0Define a function called get_prefect_subtree(), this takes rootr_type := a new RetTypeif root is ... Read More

Find the largest multiple of 3 from array of digits - Set 2 in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:28:27

187 Views

Suppose we have an array of different digits; we have to find the largest multiple of 3 that can be generated by concatenating some of the given digits in that array in any order. The answer may be very large so make it as string. If there is no answer return an empty string.So, if the input is like [7, 2, 8], then the output will be 87.To solve this, we will follow these steps −Define one 2D array d, there will be three rowssort the array digitssum := 0for initialize i := 0, when i < size of digits, ... Read More

Advertisements