Python Articles

Page 661 of 852

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 22-Dec-2020 762 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 string size that contains given substring in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 21-Dec-2020 448 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 Find Out the Maximum Points From Removals in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Dec-2020 296 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
Arnab Chakraborty
Updated on 15-Dec-2020 478 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
Arnab Chakraborty
Updated on 15-Dec-2020 519 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

Program to Find K-Largest Sum Pairs in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Dec-2020 283 Views

Suppose, we have been provided with two lists of numbers that are nums0 and nums1, and an integer k. Our goal is to find the k largest sum pairs where each pair contains one integer in nums0 and another in nums1. The sum of all of the pairs has to be returned.So, if the input is like nums1 = [8, 6, 12], nums2 = [4, 6, 8], k = 2, then the output will be 38. We have these largest pairs [12, 8] and [12, 6].To solve this, we will follow these steps −if k > len(nums0) * len(nums1) is ...

Read More

Program to Find Out the Edges that Disconnect the Graph in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Dec-2020 351 Views

Suppose we have been provided with an undirected graph that has been represented as an adjacency list, where graph[i] represents node i's neighbor nodes. We have to find the number of edges that satisfies the following condition.If the edge is removed, the graph becomes disconnected.So, if the input is like graph = [    [0, 2],    [0, 4],    [1, 2, 3],    [0, 3, 4],    [4],    [3],    [2] ], then the output will be 1.To solve this, we will follow these steps −Define a function dfs(). This will take curr, pre, dans := infinitydep[curr] := ...

Read More

Program to Find Out the Occurrence of a Digit from a Given Range in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Dec-2020 973 Views

Suppose we have been provided with two positive integers n and d where d is a digit within 0 to 9. We have to determine how many times the digit d appears within the integer numbers between 1 and n.So, if the input is like n = 45, d = 5, then the output will be 5.These numbers have the digit 5: [5, 15, 25, 35, 45].To solve this, we will follow these steps −Define a function solve(). This will take n and d as inputs.if n < 0, thenreturn 0k := floor of (n /10) − 1ans := solve(k, ...

Read More

Program to Find Out Currency Arbitrage in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Dec-2020 1K+ Views

Suppose we have one N x N table of currency exchange rates. We have to check whether there is some sequence of trades we can make or not. Now starting with some amount A of any currency, we can end up with some amount greater than A of that currency. There are no transaction costs and we can also trade fractional quantities.The value at entry [I, j] in this matrix represents the amount of currency j we can buy with one unit of currency i. Now consider currency 0 is USD, 1 is CAD and 2 is EUR. We can ...

Read More

Program to Connect a Forest in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Dec-2020 246 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
Showing 6601–6610 of 8,519 articles
« Prev 1 659 660 661 662 663 852 Next »
Advertisements