Programming Articles - Page 1486 of 3363

Check if matrix A can be converted to B by changing parity of corner elements of any submatrix in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:52:26

304 Views

Suppose we have two N X M binary matrices A and B. In a single operation, we can select a sub-matrix (at least 2x2) and convert the parity of the corner elements (flip bits). Finally, we have to check whether the matrix A can be converted to B by performing any number of operations or not.So, if the input is like100101100then the output will be True as we can perform the operation on the top left square sub-matrix of size (2x2) on mat1 to get mat2.To solve this, we will follow these steps −row := row count of mat1column := ... Read More

Check if lowercase and uppercase characters are in same order in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:50:18

577 Views

Suppose we have a string s with only lowercase or uppercase letters not numbers. We have to check whether both lowercase and uppercase letters follow the same order respectively or not. So, if a letter occurs more than once in lowercase then the occurrence of the same character in the uppercase will be same.So, if the input is like s = "piPpIePE", then the output will be True, as occurrences of lowercase letters and uppercase letters are same, and they are in the same order in lowercase and uppercase also.To solve this, we will follow these steps −lowercase := blank ... Read More

Check if linked list is sorted (Iterative and Recursive) in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:49:59

599 Views

Suppose we have a linked list we have to define two functions to check whether the linked list is sorted in non-increasing order or not. One of the method will work as iterative manner and another one in recursive manner.So, if the input is like L = [15, 13, 8, 6, 4, 2], then the output will be True.To solve this, we will follow these steps −Define a function solve_iter(). This will take headif head is null, thenreturn Truewhile next of head is not null, docurrent := headif value of current value of (next of head) is not 0 ... Read More

Check if leaf traversal of two Binary Trees is same in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:43:59

212 Views

Suppose we have two binary tree. We have to check whether leaf traversal of these two trees are same or not. As we know the leaf traversal is sequence of leaves traversed from left to right.So, if the input is likethen the output will be True as the left traversal sequence of both of the trees are same, that is [5, 7, 8].To solve this, we will follow these steps −s1 := a new list, s2 := another new listinsert r1 into s1 and r2 into s2while s1 and s2 are not empty, doif s1 is empty or s2 is ... Read More

Check if LCM of array elements is divisible by a prime number or not in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:40:50

188 Views

Suppose we have an array called nums and another value k, we have to check whether LCM of nums is divisible by k or not.So, if the input is like nums = [12, 15, 10, 75] k = 10, then the output will be True as the LCM of the array elements is 300 so this is divisible by 10.To solve this, we will follow these steps −for i in range 0 to size of nums - 1, doif nums[i] is divisible by k, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understanding − Live Demodef solve(nums, k) ... Read More

Check if item can be measured using a scale and some weights in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:40:32

295 Views

Suppose we have some weights like a^0, a^1, a^2, …, a^100, here 'a' is an integer, and we also have a weighing scale where weights can be put on both the sides of that scale. We have to check whether a particular item of weight W can be measured using these weights or not.So, if the input is like a = 4, W = 17, then the output will be True the weights are a^0 = 1, a^1 = 4, a^2 = 16, we can get 16 + 1 = 17.To solve this, we will follow these steps −found := ... Read More

Check if it possible to partition in k subarrays with equal sum in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:38:05

242 Views

Suppose we have an array of numbers called nums, and also have another value K. We have to check whether we can partition the array nums into K contiguous subarrays such that the elements sum of each subarrays is equal.So, if the input is like nums = [2, 5, 3, 4, 7] k = 3, then the output will be True as we can make three partitions like [(2, 5), (3, 4), (7)] all have equal sum 7.To solve this, we will follow these steps −n := size of numscumul_sum := cumulative sum of all elements in numstotal_sum := cumul_sum[n ... Read More

Check if it is possible to transform one string to another in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:37:38

458 Views

Suppose we have two strings s and t, t is in uppercase. We have to check whether we can convert s to t by performing following operations.Convert some lowercase letters uppercase.Remove all of the lowercase letters.So, if the input is like s = "fanToM", t = "TOM", then the output will be True as we can change ‘o’ to ‘O’ then remove all other lowercase letters from s to make it t.To solve this, we will follow these steps −n := size of s, m := size of tdp:= a matrix of size (m + 1)x(n + 1) and fill ... Read More

Check if it is possible to survive on Island in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:37:19

588 Views

Suppose there is an island. There is only one store in that location, this store remains open always except Sunday. We have following values as input −N (Maximum number of food someone can buy each day).S (Number of days someone is required to survive).M (Number of food required each day to survive).If it is Monday, and we need to survive for the next S number of days. We have to check whether we can survive or not, if we can find the minimum number of days on which we need to buy food, so that we can survive the next ... Read More

Check if it is possible to sort the array after rotating it in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:29:36

250 Views

Suppose we have a list of numbers called nums, we have to check whether we can sort nums or not by using rotation. By rotation we can shift some contiguous elements from the end of nums and place it in the front of the array.So, if the input is like nums = [4, 5, 6, 1, 2, 3], then the output will be True as we can sort by rotating last three elements and send them back to first.To solve this, we will follow these steps −n := size of numsif nums is sorted, thenreturn Trueotherwise, status := Truefor i ... Read More

Advertisements