Server Side Programming Articles - Page 1345 of 2650

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

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

435 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

533 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

223 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

Check if it is possible to sort an array with conditional swapping of adjacent allowed in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:28:47

298 Views

Suppose we have an unordered array of numbers called nums and all elements are in range 0 to n-1. We can swap adjacent elements in nums as many times as required but only when the absolute difference between these element is 1. We have to check whether we can sort the nums or not.So, if the input is like nums = [1, 0, 3, 2, 5, 4], then the output will be True as we can swap these pairs [(1, 0), (3, 2), (5, 4)] to sort [0, 1, 2, 3, 4, 5].To solve this, we will follow these steps ... Read More

Check if it is possible to serve customer queue with different notes in Python

Arnab Chakraborty
Updated on 19-Jan-2021 04:25:42

119 Views

Suppose we have an array called notes represents different rupee notes holding by customers in a queue. They are all waiting to buy tickets worth Rs 50. Here possible notes are [50, 100 and 200]. We have to check whether we can sell tickets to the people in order or not, initially we have Rs 0 in our hand.So, if the input is like notes = [50, 50, 100, 100], then the output will be True for first two we do not need to return anything, but now we have two Rs 50 notes. So for last two we can ... Read More

Check if it is possible to return to the starting position after moving in the given directions in C++

Arnab Chakraborty
Updated on 18-Jan-2021 13:33:52

210 Views

Suppose we are at position (0, 0) We have a string represents consecutive directions using four letters. We have to check whether we can return at (0, 0) position after considering all of the given directions. The symbols areE for eastW for westN for northS for south.So, if the input is like "EENWWS", then the output will be true, move east two units, then go north, then west two units then again south, so this is the starting position.To solve this, we will follow these steps −l := size of moves arrayif l is same as 0, then −return truelft ... Read More

Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:31:43

149 Views

Suppose we have a list of rectangles represented using its length and breadth. We can rotate any rectangle by 90 degrees so after rotating, the breadth will become length and vice-versa. We have to check whether we can sort the rectangles in non-increasing order of breadth.So, if the input is like rects = [[4, 5], [5, 7], [4, 6]], then the output will be True as the breadths are [5, 7, 6] now if we rotate last two rectangles then breadths will be [5, 5, 4] which is in non-increasing way.To solve this, we will follow these steps −m := ... Read More

Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:28:09

372 Views

Suppose we have a binary string s whose length is 2 or more. We have to check whether we can rearrange s such that there are alternate 0s and 1s.So, if the input is like s = "1000111", then the output will be True as we can form "1010101" from s.To solve this, we will follow these steps −one_count := count of 1 in binary string szero_count := count of 0 in binary string sif size of s is even, thenreturn true when one_count is same as zero_count otherwise falsereturn true when |one_count - zero_count| is same as 1 otherwise ... Read More

Check if it is possible to reach vector B by rotating vector A and adding vector C to its in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:26:15

115 Views

Suppose we have three vectors x, y and z in 2D plane. We have to check whether we can get vector y from vector x by rotating it 90 degrees(clockwise) or adding z with it any number of times as required.So, if the input is like x = (-4, -2) y = (-1, 2) z = (-2, -1), then the output will be True as we can add z with x to get location (-2, -1), then rotate 90° clockwise to get (-1, 2).To solve this, we will follow these steps −Define a function util() . This will take p, ... Read More

Check if it is possible to reach a number by making jumps of two given length in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:20:25

149 Views

Suppose we are at starting position p, we can jump at any direction (left or right) d1 and d2 units. We have to find minimum number of steps required to reach at position q by jumping from p.So, if the input is like p = 5, q = 10, d1 = 4, d2 = 3, then the output will be 3 as we can jump to right using distance 4 twice then we can reach at location 13, then jump left 3 units to reach 10.To solve this, we will follow these steps −gcd_res := gcd of d1 and d2if ... Read More

Advertisements