Found 33676 Articles for Programming

Program to count minimum k length sublist that can be flipped to make all items of list to 0 in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:20:49

122 Views

Suppose we have a list of numbers called nums that stored 0s and 1s. We have another value k.Now consider there is an operation where we flip a sublist of length k such that all 1s will be 0s and all 0s will be 1s. We have to find the minimum number of operations required to change nums into all 1s to 0s. If we cannot change it return -1.So, if the input is like nums = [1, 1, 1, 0, 0, 1, 1, 1], k = 3, then the output will be 2, as we can flip the first ... Read More

Program to find minimum radius to light up all houses on a street in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:18:26

699 Views

Suppose we have a list of numbers called nums that are representing position of houses on a 1-dimensional line. Now consider we have 3 street lights that we can put anywhere on the line and a light at position x lights up all houses in range [x - r, x + r], inclusive. We have to find the smallest r required to light up all houses.So, if the input is like nums = [4, 5, 6, 7], then the output will be 0.5, as we can place the lamps on 4.5, 5.5 and 6.5 so r = 0.5. So these ... Read More

Program to find minimum digits sum of deleted digits in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:19:53

168 Views

Suppose we have two strings s and t of digits, we have to find a way to remove digits in the strings so that: 1. Two strings are same 2. The sum of the digits that are deleted is minimized Finally return the minimized sum.So, if the input is like s = "41272" t = "172", then the output will be 6, as we can remove "4" and "2" from the first string to get "172".To solve this, we will follow these steps −Define a function lcs() . This will take a, b, m, ntable := a 2d matrix of ... Read More

Program to find minimum string size that contains given substring in Python

Arnab Chakraborty
Updated on 21-Dec-2020 14:08:50

410 Views

Suppose we have two strings s and t, we have to find the size of a minimum substring in s that contains all the characters of t. If there is no such substring exists then return -1.So, if the input is like s = "thegrumpywizardmakes" t = "wake", then the output will be 10, as the shortest substring that contains "wake" is "wizardmake" (length of 10).To solve this, we will follow these steps −counter := frequency of each character in bstart := 0min_subs := infrem := count of distinct characters in bfor end in range 0 to size of a, ... Read More

Program to count number of minimum swaps required to make it palindrome in Python

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

401 Views

Suppose we have a string s, we have to find the minimum number of adjacent swaps needed to make it into a palindrome. If there is no such way to solve, then return -1.So, if the input is like s = "xxyy", then the output will be 2, as we can swap the middle "x" and "y" so string is "xyxy" and then swap the first two "x" and "y" to get "yxxy", and this is palindrome.To solve this, we will follow these steps −Define a function util() . This will take sseen := a new mapfor each i in ... Read More

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

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

255 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

Advertisements