Found 33676 Articles for Programming

Program to find maximum possible value of smallest group in Python

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

282 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

432 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

Program to Find K-Largest Sum Pairs in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:09:00

243 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 the Inverted Inversions in Python

Arnab Chakraborty
Updated on 15-Dec-2020 13:06:52

167 Views

Suppose we have been provided with a list of numbers nums. We have to find the number of existing quadruplets (a, b, c, d) such that a < b < c < d and nums[a] < nums[b] and nums[c] > nums[d].The array nums is a permutation of the integers 1...NSo, if the input is like nums = [3, 4, 7, 6, 5], then the output will be 5.From the given input, we have these inverted inversions −3, 4, 7, 63, 4, 6, 53, 4, 7, 53, 7, 6, 54, 7, 6, 5To solve this, we will follow these steps −m ... Read More

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

Arnab Chakraborty
Updated on 15-Dec-2020 13:04:55

319 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
Updated on 15-Dec-2020 13:01:25

861 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
Updated on 15-Dec-2020 13:00:12

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

Advertisements