Return to Starting Position After Moving in Given Directions in C++

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

199 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

Rearrange Rectangles in Non-Ascending Order of Breadths in Python

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

140 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

Rearrange Binary String with Alternate 0s and 1s in Python

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

363 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

Reach Vector B by Rotating Vector A and Adding Vector C in Python

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

102 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

Reach a Number by Making Jumps of Two Given Lengths in Python

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

138 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

Check If Moving from (0,0) to (x,y) in n Steps is Possible in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:18:19

255 Views

Suppose we have a coordinate point (x, y) and another value n. We have to check whether we can move from (0, 0) to (x, y) using n steps or not. We can move any of the four directions left, right, up and down.So, if the input is like p = (2, 1) n = 3, then the output will be True we can move two step to the right then one step to the up direction.To solve this, we will follow these steps −if n >= |x of p| + |y of p| and (n -(|x of p| + ... Read More

Check If Two Matrices Can Be Made Strictly Increasing in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:17:14

135 Views

Suppose we have two matrices of size n x m named mat1 and mat2. We have to check where these two matrices are strictly increasing or not by swapping only two elements in different matrices only when they are at position (i, j) in both matrices.So, if the input is like7151610149817then the output will be True as we can swap (7, 14) and (10, 17) pairs to make them strictly increasing.1415161779810To solve this, we will follow these steps −row := row count of mat1col := column count of mat1for i in range 0 to row - 1, dofor j in ... Read More

Check If You Can Return to 12 O'Clock by Adding or Subtracting Seconds in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:12:17

84 Views

Suppose we have an array of n different second values. We have to check whether it is possible to start from 12'O clock and go back to 12 by only adding or subtracting the given seconds or not. We can use all given seconds exactly once, we can either add seconds or subtract it.So, if the input is like seconds = [40, 90, 50], then the output will be True as it can add 40 then subtract 90 then again add 50.To solve this, we will follow these steps −size := 2^(length of seconds array)for c in range 0 to ... Read More

Form String B from A Under Given Constraints in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:10:50

121 Views

Suppose we have two strings s and t, and two values p and q. We have to check whether it is possible to get t from s such that s is separated into groups of p number of characters except the last group which will have characters ≤ p and we can pick at most q number of characters from each group, and also order of characters in t must be same as s.So, if the input is like s = "mnonnopeqrst", t = "moprst", p = 5, q = 2, then the output will be True as we can ... Read More

Check If a Straight Line Can Be Drawn with Given Direction Cosines in Python

Arnab Chakraborty
Updated on 18-Jan-2021 13:08:58

222 Views

Suppose we have three direction cosines l, m and n in 3-D space, we have to check whether it is possible to draw a straight line with these direction cosines or not.So, if the input is like l = 0.42426 m = 0.56568 n = 0.7071, then the output will be True as this the direction cosines of a vector {3, 4, 5}.To solve this, we will follow some rule asl = cos(a), where a is angle between the straight line and x-axism = cos(b), where b is angle between the straight line and y-axisn = cos(c), where c is ... Read More

Advertisements