Find Maximum Number of K-Sum Pairs in Python

Arnab Chakraborty
Updated on 06-Oct-2021 10:45:25

597 Views

Suppose we have an array called nums and another value k. In one operation, we can select two elements from nums whose sum is equals to k and remove them from the array. We have to find the maximum number of operations we can perform on the array.So, if the input is like nums = [8, 3, 6, 1, 5] k = 9, then the output will be 2 as we can delete [3, 6] whose sum is 9, then remove [8, 1] whose sum is also 9.To solve this, we will follow these steps −counter := a map holding ... Read More

Find Maximum Score in Stone Game using Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:45:14

346 Views

Suppose there are several stones placed in a row, and each of these stones has an associated number which is given in an array stoneValue. In each round Amal divides the row into two parts then Bimal calculates the value of each part which is the sum of the values of all the stones in this part. Bimal throws away the part which has the maximum value, and Amal's score increases by the value of the remaining part. When the values of two parts are same, Bimal lets Amal decide which part will be thrown away. The next round starts ... Read More

Check String Transformability with Substring Sort in Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:36:07

159 Views

Suppose we have two numeric strings s and t, we want to transform from string s to t using the following operation any number of times: 1. Select a non-empty substring in s and sort it inplace so the characters are in ascending order. We have to check whether it is possible to transform string s into string t or not.So, if the input is like s = "95643" t = "45963", then the output will be True because we can transform s into t using like "95643" -> "95463" -> "45963".To solve this, we will follow these steps −places ... Read More

Find Out if the Graph is Traversable by Everybody in Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:32:25

193 Views

Suppose, we are given a graph that contains n vertices numbered 0 to n - 1. The graph is undirected and each edge has a weight. The graph can have three types of weights and each weight signifies a particular task. There are two people that can traverse the graph, namely Jack and Casey. Jack can traverse the graph if an edge has weight 1, Casey can traverse the graph if it has weight 2, and both can traverse the graph if it has edge weight 3. We have to remove any edges necessary to make the graph traversable for ... Read More

Detect Cycles in 2D Grid using Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:27:24

281 Views

Suppose we have one 2D array of characters called grid of size m x n. We have to check whether we can detect a cycle inside it or not. Here a cycle is a path of length 4 or more in the grid that starts and ends at the same position. We can move in four directions (up, down, left, or right), if it has the same value of the current cell, and we cannot revisit some cell.So, if the input is likemmmpmkmmmmsmftmmthen the output will be True, because the green cells are forming cycle.To solve this, we will follow ... Read More

Find Minimum Days to Eat N Oranges in Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:13:33

300 Views

Suppose we have a number n. So consider there are n oranges in the kitchen and we eat some of these oranges every day maintaining these rules: 1. Eat single orange. 2. If n is even, then eat n/2 oranges. 3. If n is divisible by 3 can eat 2*(n/3) oranges. We can select only one option each day. We have to find the minimum number of days to eat n oranges.So, if the input is like n = 10, then the output will be 4 becauseOn day 1 eat 1 orange, 10 - 1 = 9.On day 2 eat ... Read More

Find Minimum Cost to Cut a Stick in Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:09:38

805 Views

Suppose we have a value n and an array called cuts. Consider there is a wooden stick of length n units. The stick is labelled from 0 to n. Here cuts[i] represents a position where we can cut. We should perform the cuts in order, but we can change the order of the cuts as we want. Here the cost of one cut is the size of the stick to be cut, and the total cost is the sum of costs of all cuts. We have to find the minimum total cost of the cuts.So, if the input is like ... Read More

Find Longest Awesome Substring in Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:06:54

552 Views

Suppose we have a numeric string s. As we know an awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it palindrome. We have to find the length of the maximum length awesome substring of s.So, if the input is like s = "4353526", then the output will be 5 because "35352" is the longest awesome substring. we can make "35253" palindrome.To solve this, we will follow these steps −n := 0pos_map := a map containing a key 0 and corresponding value is size of smax_len := 1for ... Read More

Find Maximum Score from All Possible Valid Paths in Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:00:33

598 Views

Suppose we have two arrays nums1 and nums2. A valid path is defined as follows −Select nums1 or nums2 to traverse (from index-0).Traverse the array from left to right.Now, if we are moving through any value that is present in nums1 and nums2 we can change the path to the other array. Here the score is the sum of unique values in a valid path. We have to find the maximum score we can get of all possible valid paths. If the answer is too large then return result modulo 10^9+7.So, if the input is like nums1 = [3, 5, ... Read More

Find Minimum Length of Run Length Encoding After Removing K Characters in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:57:34

247 Views

Suppose we have a string s and another value k. We can delete at most k characters from s such that the length of run-length encoded version of s is minimum. As we know the run-length encoding is a string compression method that replaces consecutive identical characters (2 or more times) with the concatenation of the character and the number marking the count of the characters. For example, if we have a string "xxyzzz" then we replace "xx" by "x2" and replace "zzz" by "z3". So compressed string is now "x2yz3". So in this problem we have to find the ... Read More

Advertisements