Convert Gray Code for a Given Number in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:59:12

922 Views

Suppose we have a number n, we have to find the gray code for that given number (in other words nth gray code). As we know the gray code is a way of ordering binary numbers such that each consecutive number's values differ by exactly one bit. Some gray codes are: [0, 1, 11, 10, 110, 111, and so on]So, if the input is like n = 12, then the output will be 10 as the 12 is (1100) in binary, corresponding gray code will be (1010) whose decimal equivalent is 10.To solve this, we will follow these steps:Define a ... Read More

Recover Shuffled Queue of People in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:57:07

158 Views

Suppose we have a 2D matrix where each row contains two values [height, count] these indicates the person has given height and there are 'count' number of people in front of them that are at least as tall as them. Now consider this queue is shuffled, we have to recover the original ordering of the queue.So, if the input is like224050then the output will be405022To solve this, we will follow these steps:N := row count of matrixrearrange matrix rows based on increasing height and decreasing countans := make a list of size N and initially all entries are nullfor each ... Read More

Find Number of Distinct Quadruples that Form Target Sum in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:52:43

380 Views

Suppose we have four lists of numbers A, B, C, and D, and also have a target value, we have to find the number of distinct quadruple (i, j, k, l) such that A[i] + B[j] + C[k] + D[l] is same as target.So, if the input is like A = [5, 4, 3] B = [8, 4] C = [6, 2] D = [4, 10] target = 23, then the output will be 3, the quadruples are [5, 8, 6, 4] [3, 4, 6, 10] [3, 8, 2, 10].To solve this, we will follow these steps:count := 0m := ... Read More

Find State of Prison Cells After K Days in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:50:07

383 Views

Suppose we have a binary list (1s and 0s in the list) and another value k. Each value in nums represents the state of a prison cell where 1 indicates occupied cell and 0 indicates empty cell. On each day when a cell has two adjacent cells that are either both occupied or both empty, then it becomes occupied. Otherwise, it becomes empty. So we have to find state of the prison cells after k number of days.So, if the input is like nums = [1, 0, 1, 0, 0, 0, 0, 0] k = 1, then the output will ... Read More

Check If List Can Be Partitioned into Pairs Where Sum is Multiple of K in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:45:27

92 Views

Suppose we have a list of numbers called nums and another value k, we have to check whether the list can be partitioned into pairs such that the sum of each pair is divisible by k.So, if the input is like nums = [4, 7, 2, 5] k = 6, then the output will be True, as we can partition the given list into pairs like (4, 2) and (8, 1) and their sums are divisible by 3.To solve this, we will follow these steps:if nums has even number of elements, thenreturn Falsecount := a list of size k and ... Read More

Find Number of Sublists to Partition a List in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:42:41

183 Views

Suppose we have a list of numbers called nums. We can partition the list into some individual sublists then sort each piece. We have to find the maximum number of sublists we can partition to such that nums as a whole is sorted afterwards.So, if the input is like nums = [4, 3, 2, 1, 7, 5], then the output will be 2, as we can sort the sublists like [4, 3, 2, 1] and [7, 5]To solve this, we will follow these steps:count := 0main_sum := 0, sorted_sum := 0for each element x from nums and y from sorted ... Read More

Find Number of Ways to Split a Palindrome in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:40:20

357 Views

Suppose we have a string s, we have to find the number of ways we can partition the string such that each part is a palindrome.So, if the input is like s = "xyyx", then the output will be 3, as we have splits like: ["x", "yy", "x"], ["x", "y", "y", "x"], ["xyyx"].To solve this, we will follow these steps:n := size of stable := a list of size n + 1 and fill it with 0table[0] := 1for i in range 0 to n, dofor j in range 0 to i - 1, dosub := s[from index j to ... Read More

Generate All Possible Strings by Taking Choices in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:36:28

438 Views

Suppose we have a string s of lowercase alphabet characters, other characters like "[", "|", and "]". Here "[a|b|c]" indicates either "a", "b", or "c" can be chosen as a possibility. We have to find a list of strings containing all possible values that s can represent. Here "[]" cannot be nested and may have any number of choices.So, if the input is like s = "[d|t|l]im[e|s]", then the output will be ['dime', 'dims', 'lime', 'lims', 'time', 'tims']To solve this, we will follow these steps:if s is empty, thenreturn a list with blank stringn := size of sseq := a ... Read More

Find Minimum Possible Maximum Value After K Operations in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:30:47

1K+ Views

Suppose we have a list of numbers called nums and another value k. Now let us consider an operation where we can subtract 1 from any element in the list. We can perform this operation k times. We have to find the minimum possible maximum value in the list after k such operations.So, if the input is like nums = [3, 4, 6, 5] k = 6, then the output will be 3, as we can decrease 4 once, 6 thrice and 5 twice to get [3, 3, 3, 3].To solve this, we will follow these steps:sort numbers in reverse ... Read More

Find Number of Only Child in a Binary Tree in Python

Arnab Chakraborty
Updated on 25-Nov-2020 13:05:33

531 Views

Suppose we have a binary tree; we have to find the number of nodes that are an only child. As we know a node x is called an only child node when its parent has exactly one child that is x.So, if the input is likethen the output will be 2 as 8 and 6 are the only children.To solve this, we will follow these steps:if root is null, thenreturn 0d := a double ended queueinsert root at the end of dcount := 0while d is not empty, docurrent := left element of d and delete left elementif left of ... Read More

Advertisements