Found 10476 Articles for Python

Program to make the XOR of all segments equal to zero in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:40:13

329 Views

Suppose we have an array called nums and another value k. The XOR of a segment [left, right] (left 0, thenfor each index j and value prev in dp, donew_dp[i XOR j] := maximum of new_dp[i XOR j] and prev+cntdp := new_dpreturn size of nums - new_dp[0]ExampleLet us see the following implementation to get better understandingdef solve(nums, k):    LIMIT = 2**10    temp = [[0 for _ in range(LIMIT)] for _ in range(k)]    for i, x in enumerate(nums):       temp[i%k][x] += 1 dp = [-2000 for _ in range(LIMIT)] ... Read More

Program to find length of longest chunked palindrome decomposition in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:37:58

150 Views

Suppose we have a text. We have to find the largest possible k such that there exists a[1], a[2], ..., a[k] such that: Each a[i] is a string which is not blank. And their concatenation a[1] + a[2] + ... + a[k] is equal to the given text; For all i in range 1 to k, a[i] = a[{k+1 - i}].So, if the input is like text = "antaprezatepzapreanta", then the output will be 11 because we can split it like "a|nt|a|pre|za|tpe|za|pre|a|nt|a".To solve this, we will follow these steps −counter := 0i := 1, j := size of text - ... Read More

Program to find max chunks to make array sorted in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:34:58

239 Views

Suppose we have an array nums, we have to split the array into some number of partitions, and individually sort each of them. Now after concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made?So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5] .To solve this, we will follow these steps −real:= sort the list numsp1 := 0, p2 := 1, c := 0Do the following infinitely, doflag:= Truetmp:= sort ... Read More

Program to find out if a vertex in an undirected graph has a lesser cost path in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:40:01

171 Views

Suppose, we are given a weighted, undirected graph. We have to implement a function query that takes two vertices and a cost 'limit' as input and checks if there exists a lower cost path than the cost given as input. We return true if there exists a path or otherwise, we return false.So, if the input is likeand the queries are (0, 2, 10), (3, 1, 30), (4, 3, 30).then the output will beFalse True TrueThe result of the first query is False because there is no path between vertex 0 to 2 of cost 10.The result of the second ... Read More

Program to find maximize palindrome length from subsequences in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:30:15

177 Views

Suppose we have two strings, s and t. We want to make a string in the following manner −Select some non-empty subsequence sub1 from s.Select some non-empty subsequence sub2 from t.Concatenate sub1 and sub2, to make the string.We have to find the length of the longest palindrome that can be formed in the described manner. If we cannot make any palindrome, then return 0.So, if the input is like s = "hillrace" t = "cargame", then the output will be 7 because we can take "race" from s and "car" from r, so "racecar" is the palindrome with length 7.To ... Read More

Program to check whether one point can be converted to another or not in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:26:55

151 Views

Suppose we have a starting points (sx, sy), and target point (tx, ty), we have to check whether a sequence of moves exists from the start point to the end point. Here move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).So, if the input is like (sx, sy) = (1, 1) (tx, ty) = (4, 5), then the output will be True, this is because move (1, 1) to (2, 1), then (3, 1), then (4, 1), then (4, 5).To solve this, we will follow these steps −Define a function solve() ... Read More

Program to find out the sum of evenly spaced elements in an array in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:33:29

334 Views

Suppose, there is an array 'nums' of size n containing positive integers. We have another array 'queries' that contain integer pairs (pi, qi). For every query in the array queries, the answer will be the sum of numbers in the array nums[j] where pi

Program to count the number of ways to distribute n number of candies in k number of bags in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:27:51

563 Views

Suppose, there are n number of candies and k bags in which the candies have to put into. We have to find out the number of possible ways the candies can be distributed so that each bag contains at least one candy. Every candy in this scenario is unique, so we have to count all the possible ways the candies can be distributed in the bags.So, if the input is like n = 3, k = 2, then the output will be 3.The candies can be put in this manner −(1, 2), (3) (1) , (2, 3) (2), (1, 3)To ... Read More

Program to find maximum subarray min-product in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:18:59

291 Views

Suppose we have an array nums, we have to find the maximum min-product of each non-empty subarray of nums. Since the answer can be big enough, return it in modulo 10^9+7. The min-product of the array is equal to the minimum value in the array multiplied by the sum of the array. So if we have an array is like [4, 3, 6] (minimum value is 3) has a min-product of 3*(4+3+6) = 3*13 = 39.So, if the input is like nums = [2, 3, 4, 3], then the output will be 30 because we can get subarray [3, 4, ... Read More

Program to find maximum distance between a pair of values in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:16:10

682 Views

Suppose we have two arrays (non-growing) nums1 and nums2. The index pair (i, j) with 0

Advertisements