Find Maximum Score of a Good Subarray in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:43:00

505 Views

Suppose we have an array called nums and a value k. Consider the score of a subarray (i, j) is defined as minimum of subarray nums[i..j] * (j-i+1). Now, a good subarray is a subarray where i -1 and nums[i] >= minNum, doi := i - 1while j < size of nums and nums[j] >= minNum, doj := j + 1ans := maximum of ans and ((j - i - 1) * minNum)minNum := maximum of (nums[i] if i > -1 otherwise -1) and (nums[j] if j < size of nums otherwise -1)return ansExampleLet us see the following implementation ... Read More

Make XOR of All Segments Equal to Zero in Python

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

346 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

Find Lesser Cost Path for Vertex in Undirected Graph using Python

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

181 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

Find Length of Longest Chunked Palindrome Decomposition in Python

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

159 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

Find Max Chunks to Make Array Sorted in Python

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

251 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

Sum of Evenly Spaced Elements in an Array in Python

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

345 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

Maximize Palindrome Length from Subsequences in Python

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

189 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

Count Ways to Distribute Candies in Bags in Python

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

573 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

Check Point Conversion in Python

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

161 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

Find Smallest Value of K for K-Similar Strings in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:24:31

258 Views

Suppose we have two strings s and t. These two strings are K-similar if we can swap the positions of two letters in s exactly K times so that the resulting string is t. So, we have two anagrams s and t, we have to find the smallest K for which s and t are K-similar.So, if the input is like s = "abc" t = "bac", then the output will be 1.To solve this, we will follow these steps −Define a function neighbors() . This will take new_datafor each index i and value c in new_data, doif c is ... Read More

Advertisements