Connect a Forest in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:57:59

236 Views

Suppose we have graphs as an adjacency lists. This graph is actually a set of disjoint trees. We have to add a certain number of edges to the forest such that it becomes a single tree. We have to return the minimum distance possible of the longest path between any two nodes. So, if the input is likethen the output will be 4.We can add the edge 0 −> 5. Then, the longest path can be any of 3 −> 1 −> 0 −> 5 −> 7 or 4 −> 1 −> 0 −> 5 −> 7; and also these ... Read More

Find the Best Interval to Remove in C++

Arnab Chakraborty
Updated on 15-Dec-2020 12:54:00

218 Views

Suppose we have a list of intervals (inclusive) that are potentially overlapping. Now consider there is an operation where we delete one interval, then merge the remaining intervals and then count the number of intervals left over. We have to find the maximum number of leftover intervals possible after removal.So, if the input is like intervals = [ [5, 8], [6, 7], [7, 10], [9, 11]], then the output will be 2. This is because −If we delete the interval [5, 8] we get [6, 11] as the merge.If we delete the interval [6, 7] we get [5, 11] as ... Read More

Efficient Way to Study in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:51:27

111 Views

Suppose, we have three lists whose lengths are same. These are deadlines, credits, and durations. They are representing course assignments. For the i−th assignment deadlines[i] shows its deadline, credits[i] shows its credit, and durations[i] shows the number of days it takes to finish the assignment. One assignment must be completed before starting another one. We have to keep in mind that we can complete an assignment on the day it's due and we are currently on the start of day 0.So, if the input is like, deadlines = [7, 5, 10], credits = [8, 7, 10], durations = [5, 4, ... Read More

Find Maximum Value by Inserting Operators in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:49:50

370 Views

Suppose we have a list of numbers called nums, we have to find the maximal value that can be generated by adding any binary operators like +, −, and * between the given numbers as well as insert any valid brackets.So, if the input is like nums = [−6, −4, −10], then the output will be 100, as we can make the expression like: ((−6) + (−4)) * −10.To solve this, we will follow these steps −OPS := a list of operators [+, −, *]N := size of Aif all elements in A is 0, thenreturn 0Define a function dp() ... Read More

Concatenate Words to Make Palindromes in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:47:31

202 Views

Suppose we have a list of distinct words, we have to find the number of different ways we can concatenate two different words from the given list of words to make a palindrome.So, if the input is like words = ["time", "emit", "mo", "m"], then the output will be 3, as we can make "timeemit", "emittime", and "mom".To solve this, we will follow these steps −res := 0ln := number of words in the arrayfor k in range 0 to 1, dofor i in range 0 to ln − 1, dofor j in range i + 1 to ln − ... Read More

Find Max Values of Sublists of Size K in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:43:55

216 Views

Suppose we have a list nums and another value k, we have to find the maximum values of each sublist of size k.So, if the input is like nums = [12, 7, 3, 9, 10, 9] k = 3, then the output will be [12, 9, 10, 10]To solve this, we will follow these steps −if k > size of nums, thenreturn a blank listres := a new listtemp := nums[0]temp := npoint := 0for i in range 0 to k − 1, doif nums[i] > temp, thentemp := nums[i]point := iinsert temp at the end of resfor i in ... Read More

Find Length of Longest Sublist Whose Sum is 0 in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:41:36

158 Views

Suppose we have a list with only two values 1 and −1. We have to find the length of the longest sublist whose sum is 0.So, if the input is like nums = [1, 1, −1, 1, 1, −1, 1, −1, 1, −1], then the output will be 8, as the longest sublist is [−1, 1, 1, −1, 1, −1, 1, −1] whose sum is 0.To solve this, we will follow these steps −table := a new empty mapcs := 0, max_diff := 0for i in range 0 to size of nums − 1, docs := cs + nums[i]if cs ... Read More

Find Length of Longest Substring with K Distinct Characters in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:39:26

785 Views

Suppose we have a number k and another string s, we have to find the size of the longest substring that contains at most k distinct characters.So, if the input is like k = 3 s = "kolkata", then the output will be 4, as there are two longest substrings with 3 distinct characters these are "kolk" and "kata", which have length 4.To solve this, we will follow these steps −ans := 0, left := 0table := a new mapfor right is in range 0 to size of s − 1, dotable[s[right]] := 1 + (s[right] if exists otherwise 0)if ... Read More

Find Length of Longest Substring with Even Vowel Counts in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:36:37

446 Views

Suppose we have a string s (lowercase), we have to find the length of the longest substring where each vowel occurs even number of times.So, if the input is like s = "anewcoffeepot", then the output will be 10, as the substring "wcoffeepot" has two vowels "o" and "e", both of which occurs two times.To solve this, we will follow these steps −vowels := a map assigning vowels and numeric values as {a:0, e:1, i:2, o:3, u:4}prefix := an empty map and insert a key-value pair (0, −1) into itmask := 0, n := size of s, res := 0for ... Read More

Length of Longest Palindromic Substring After Single Rotation in Python

Arnab Chakraborty
Updated on 15-Dec-2020 12:34:46

239 Views

Suppose we have a string s, which we can rotate at any point exactly once. We have to find the length of the longest palindromic substring we can get by doing this operation.So, if the input is like s = "elklev", then the output will be 7, as we can rotate between "el" and "klev" to get "levelk". So here the longest palinfromic substring length is 5.To solve this, we will follow these steps −s2 := concatenate s twicemax_len := 0for x in range 0 to size of s − 1, dofor y in range 0 to size of s, ... Read More

Advertisements