Find K for KxK Square of Same Value in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:21:22

175 Views

Suppose we have a 2d matrix, we have to find the largest k × k submatrix where all of its elements are containing the same value, then find the value of k.So, if the input is like1183155525554555then the output will be 3, as there is a 3 × 3 square matrix of value 5.To solve this, we will follow these steps −n := row count of matrixm := column count of matrixDefine one 2D array dp of size (n x m) and fill with 1ret := 1for initialize i := n - 1, when i >= 0, update (decrease i ... Read More

Count Number of Palindromes After Minimum Splits in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:18:41

147 Views

Suppose we have a lowercase string s, we have to split it into as few strings as possible such that each string is a palindrome and then find the number of strings.So, if the input is like s = "levelracecar", then the output will be 2, as there are two palindromes "level" and "racecar".To solve this, we will follow these steps −n := size of ADefine an array result of size (n + 1)result[n] := -1for initialize i := n - 1, when i >= 0, update (decrease i by 1), do −result[i] := n - i - 1for initialize ... Read More

Find Minimum Largest Sum of K Sublists in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:17:00

182 Views

Suppose we have a list of numbers called nums and another value k. We can split the list into k non-empty sublists. We have to find the minimum largest sum of the k sublists.So, if the input is like nums = [2, 4, 3, 5, 12] k = 2, then the output will be 14, as we can split the list like: [2, 4, 3, 5] and [12].To solve this, we will follow these steps −Define a function ok(), this will take array v, k, x,cnt := 0, sum := 0for each element i in v −if sum + i > x, then −sum := i(increase cnt by 1)Otherwisesum := sum + ireturn true when cnt

Minimum Cost to Increase Heights of Trees in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:14:54

244 Views

Suppose we have a list of numbers called heights that represents the height of plants and we have another list of values called costs that represents the price needed to increase height of a plant by one. We have to find the smallest cost to make each height in the heights list different from adjacent heights.So, if the input is like heights = [3, 2, 2] costs = [2, 5, 3], then the output will be 3, as we can increase the last height by 1, which costs 3.To solve this, we will follow these steps −Define a function dp() ... Read More

Find Maximum Value of K for Safe Distance in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:13:10

163 Views

Suppose we have a binary matrix. Here 0 signifies an empty cell, and a 1 signifies a cell with a person. The distance between two cells is the maximum value between the difference in x coordinates and the difference in y coordinates. Now matrix is considered safe with a factor k if there is an empty square such that the distance from the cell to each person in the matrix, and each side of the matrix is all greater or equal to k. We have to find the maximum value of factor k for which we can be safe.So, if ... Read More

Find Size of Smallest Sublist Whose Sum is At Least Target in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:10:54

205 Views

Suppose we have a list of numbers called nums, and an another input called target, we have to find the size of the shortest sublist such that its sum value is same as target or larger. If there is no such sublist then return -1.So, if the input is like nums = [2, 11, -4, 17, 4] target = 19, then the output will be 2, as we can select [17, 4] to get sum of at least 19.To solve this, we will follow these steps −ps := a list with only one element 0for each num in nums, doinsert ... Read More

Find Smallest Difference Between Elements from Different Lists in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:08:58

172 Views

Suppose we have a list of lists, we have to find the smallest difference that can be formed by picking one value from each of the lists and taking the difference between the maximum and the minimum number of the picked element.So, if the input is like lists = [ [30, 50, 90], [85], [35, 70]], then the output will be 20, as we can take 90, 85, 70 and 90 - 70 = 20To solve this, we will follow these steps −maxVal := -infret := infdefine a priority queue pqn := size of listsfor initialize i := 0, when ... Read More

Find Minimum Digit Sum of Deleted Digits in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:19:53

168 Views

Suppose we have two strings s and t of digits, we have to find a way to remove digits in the strings so that: 1. Two strings are same 2. The sum of the digits that are deleted is minimized Finally return the minimized sum.So, if the input is like s = "41272" t = "172", then the output will be 6, as we can remove "4" and "2" from the first string to get "172".To solve this, we will follow these steps −Define a function lcs() . This will take a, b, m, ntable := a 2d matrix of ... Read More

Count Minimum Swaps Required to Make a String Palindrome in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:19:13

401 Views

Suppose we have a string s, we have to find the minimum number of adjacent swaps needed to make it into a palindrome. If there is no such way to solve, then return -1.So, if the input is like s = "xxyy", then the output will be 2, as we can swap the middle "x" and "y" so string is "xyxy" and then swap the first two "x" and "y" to get "yxxy", and this is palindrome.To solve this, we will follow these steps −Define a function util() . This will take sseen := a new mapfor each i in ... Read More

Find Minimum Possible Difference of Indices of Adjacent Elements in Python

Arnab Chakraborty
Updated on 22-Dec-2020 10:18:31

253 Views

Suppose we have a list of numbers nums, we can say that two numbers nums[i] ≤ nums[j] are adjacent when there is no number in between (nums[i], nums[j]) in nums. We have to find the minimum possible |j - i| such that nums[j] and nums[i] are adjacent.So, if the input is like nums = [1, -9, 6, -6, 2], then the output will be 2, as we can see that 2 and 6 are adjacent and they are 2 indices away from each other.To solve this, we will follow these steps −indexes := a new mapfor each index i and ... Read More

Advertisements