Found 10476 Articles for Python

Program to find minimum possible difference of indices of adjacent elements in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:18:31

253 Views

Suppose we have a list of numbers nums, we can say that two numbers nums[i] ≤ nums[j] are adjacent when there is no number in between (nums[i], nums[j]) in nums. We have to find the minimum possible |j - i| such that nums[j] and nums[i] are adjacent.So, if the input is like nums = [1, -9, 6, -6, 2], then the output will be 2, as we can see that 2 and 6 are adjacent and they are 2 indices away from each other.To solve this, we will follow these steps −indexes := a new mapfor each index i and ... Read More

Program to count number of words we can generate from matrix of letters in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:17:50

272 Views

Suppose we have a 4 x 4 board of letters and a list of words, we have to find the largest number of words that can be generated in the board by a sequence of adjacent letters, using one cell at most once per word (but we can reuse cells for other words). We can go up, down, left, right, or diagonal direction.So, if the input is likembfdxayatztrsqqqwords = ["bat", "far", "mat"], then the output will be 3, as we can generate mat [0, 1] → [1, 1] → [2, 0], bat [0, 2] → [1, 1] → [2, 2], ... Read More

Program to find intervals by merging target interval in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:16:58

163 Views

Suppose we have a list of non-overlapping intervals. These are sorted based on end time. We have another interval target, find final interval after merging target so that intervals are still non-overlapping and sorted.So, if the input is like intervals = [[1, 15],[25, 35],[75, 90]], target = [10, 30], then the output will be [[1, 35], [75, 90]] as first two intervals [1, 15] and [25, 35] are merged.To solve this, we will follow these steps −insert target at the end of ivsort iv based on start timeres := a new list with first intervali := 1while i < size of iv, doif start time of iv[i]

Program to find list of elements which are less than limit and XOR is maximum in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:16:11

189 Views

Suppose we have a list of numbers nums and a list of queries where each query contains [x, limit]. We have to find a list such that for each query [x, limit], we find an element e in nums such that e ≤ limit and e XOR x is maximized. If there is no such element, return -1.So, if the input is like nums = [3, 5, 9] queries = [[4, 6], [2, 0]], then the output will be [3, -1], as for the first query, we can use 2 or 4 in nums. 3 ^ 4 = 7 while ... Read More

Program to find length of subsequence that can be removed still t is subsequence of s in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:15:18

210 Views

Suppose we have a string s and another string t. And t is a subsequence of s. We have to find the maximum length of a substring that we can remove from s so that, t is still a subsequence of s.So, if the input is like s = "xyzxyxz" t = "yz", then the output will be 4, as We can remove the substring "abca"To solve this, we will follow these steps −left := a new list, right := also a new listc1 := -1, c2 := -1, c3 := -1j := 0for i in range 0 to size ... Read More

Program to find maximum possible value of smallest group in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:14:34

281 Views

Suppose we have a list of numbers called nums and another value k. We have to split the list into k contiguous groups. The smallest group is the one whose sum is the smallest out of all the groups. So find the maximum possible value of the smallest group.So, if the input is like nums = [2, 6, 4, 5, 8] k = 3, then the output will be 8, as we can split the list into three groups like: [2, 6], [4, 5], [8]. So the minimum group has sum 8.To solve this, we will follow these steps −Define ... Read More

Program to find how max score we can get by removing 10 or 01 from binary string in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:13:26

363 Views

Suppose we have a binary string s and two values zero_one and one_zero. Now let us consider an operation where we can delete any substring "01" and receive zero_one points. Or we can remove any substring "10" and receive one_zero points. We have to find the maximum number of points we can get after any number of operations.So, if the input is like s = "10100101" zero_one = 3 one_zero = 2, then the output will be 11, as we can remove "01" three times to get 3*3 = 9 points. Then the remaining string is 10. By removing this ... Read More

Program to Find Out the Maximum Points From Removals in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:16:35

265 Views

Suppose we are provided with a list of positive numbers. Now, here we can remove any contiguous sub list of some length t with the same value and get points t * t. One condition is to be considered, that we can perform this any number of times until the list is empty. So we have to determine the maximum number of points we can get.So, if the input is like nums = [4, 4, 6, 4, 4], then the output will be 17.For the output, we can first remove the 6 which has length 1 and yields 1 * ... Read More

Program to Find Out the Largest K-Divisible Subsequence Sum in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:14:03

431 Views

Suppose we are given a list of non−negative numbers, and a positive value k. We have to find the maximum sum subsequence of numbers such that the sum is divisible by k.So, if the input is like, nums = [4, 6, 8, 2], k = 2, then the output will be 20.The sum of the whole array is 20, which is divisible by 2.To solve this, we will follow these steps −numsSum := sum of the values in input list numsremainder := numsSum mod kif remainder is same as 0, thenreturn numsSumsort the list numsfor each number combination tpl in ... Read More

Program to Find the longest subsequence where the absolute difference between every adjacent element is at most k in Python.

Arnab Chakraborty
Updated on 15-Dec-2020 13:12:14

478 Views

Suppose we are given a list of numbers and another value k. This time our task is to find the length of the longest subsequence where the absolute difference between every adjacent element is at most k.So, if the input is like nums = [5, 6, 2, 1, −6, 0, −1, k = 4, then the output will be 6.To solve this, we will follow these steps −Define a function update() . This will take i, xi := i + nwhile i is non−zero, dosegtree[i] := maximum of segtree[i], xi := i / 2Define a function query() . This will ... Read More

Advertisements