Programming Articles - Page 1019 of 3363

Program to count pairs with XOR in a range in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:53:47

513 Views

Suppose we have an array nums and have two values l and r, we have to find the number of nice pairs. Here a nice pair is a pair (i, j) where 0 >= 1       return res // 2    return test(nums, r + 1) - test(nums, l) nums = [4,1,7,2] l = 2 r = 6 print(solve(nums, l, r))Input[4,1,7,2], 2, 6 Output6

Program to find maximize score after n operations in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:50:20

514 Views

Suppose we have an array called nums, whose size is 2*n. We have to perform n operations on this array. In the ith operation (1-indexed), we will do the following:Select two elements, x and y.Get a score of i*gcd(x, y).Remove x and y from the array nums.We have to find the maximum score we can get after performing n operations.So, if the input is like nums = [6, 2, 1, 5, 4, 3], then the output will be 14 because the optimal choices are (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 ... Read More

Program to find out the maximum value of a 'valid' array in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:45:25

265 Views

Suppose, we have an array of n integers 'nums'. Each value in 'nums' represent its 'power'. The array will be evaluated 'valid' if the length of the array is greater than two and the first and last value of the array is equal. We have to make the array valid by deleting elements from the array so that the rest can satisfy the condition. As output, we return the maximum possible power value of the array by adding all the power values of the array.So, if the input is like nums = [3, 4, 5, 3, 4], then the output ... Read More

Program to find maximum score of a good subarray in Python

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

521 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

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

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

358 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

172 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

269 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

200 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

203 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

171 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

Advertisements