Letter Tile Possibilities in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:24:26

606 Views

Suppose we have a set of tiles, where each tile has one letter tiles[i] printed on it. Find the number of possible non-empty sequences of letters that we can make. So if the input is “AAB”, then the output will be 8. As sequences are “A”, “B”, “AA”, “AB”, “BA”, “AAB”, “ABA”, “BAA”To solve this, we will follow these steps −Define one dfs(), that will take countsum := 0for i in range 1 to 26if count[i] = 0, then go for next iteration, without checking the restdecrease count[i] by 1, and increase sum by 1sum := sum + dfs(count)increase count[i] ... Read More

Flip Columns for Maximum Number of Equal Rows in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:20:01

267 Views

Suppose we have a matrix consisting of 0s and 1s, we can choose any number of columns in the matrix and flip every cell in that column. converting a cell changes the value of that cell from 0 to 1 or from 1 to 0. we have to find the maximum number of rows that have all values equal after some number of flips. So if the matrix is like −000001110The output will be 2. This is because after converting values in the first two columns, the last two rows have equal values.To solve this, we will follow these steps ... Read More

Distant Barcodes in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:16:13

175 Views

Suppose in a warehouse, there is a row of barcodes. The i-th barcode is barcodes[i]. We have to rearrange the barcodes so that no two adjacent barcodes are same. So if the input is [1, 1, 1, 2, 2, 2] so output is [2, 1, 2, 1, 2, 1].To solve this, we will follow these steps −make one map named dstore the frequency of numbers present in the barcode array into dx := empty listinsert all key-value pairs into xi := 0res := make a list whose length is same as barcodes, and fill [0]sort x based on the frequencywhile ... Read More

Previous Permutation with One Swap in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:09:47

434 Views

Suppose we have an array A of positive integers (not necessarily unique), we have to find the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]). If it cannot possible, then return the same array. So if the array is like [3, 2, 1], then the output will be [3, 1, 2], by swapping 2 and 1To solve this, we will follow these steps −n := size of Afor left in range n – 2 down to -1if left = -1, then return ... Read More

Partition Array for Maximum Sum in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:06:21

1K+ Views

Suppose we have an integer array A, we have to partition the array into (contiguous) subarrays of length at most K. After partitioning, each subarray has their values changed to become the maximum value of that subarray. We have to find the largest sum of the given array after partitioning. So if input is like [1, 15, 7, 9, 2, 5, 10] and k = 3, then the output will be 84. This is because the array becomes [15, 15, 15, 9, 10, 10, 10]To solve this, we will follow these steps −make one array dp of length same as ... Read More

Minimum Flips to Make A or B Equal to C in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:59:36

675 Views

Suppose we have 3 positives numbers a, b and c. We have to find the minimum flips required in some bits of a and b to make (a OR b == c ). Here we are considering bitwise OR operation.The flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation. So if a : 0010 and b := 0110, so c is 0101, After flips, a will be 0001, and b will be 0100To solve this, we will follow these steps −ans := 0for i in range 0 ... Read More

Sum of Nodes with Even Valued Grandparent in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:51:56

193 Views

Suppose we have a binary tree, we have to find the sum of values of nodes with even-valued grandparent. (A grandparent of a node is the parent of its parent, if it exists.). If there are no such nodes with an even-valued grandparent, then return 0. So if the tree is like −The output will be 18. The red nodes are nodes with even-value grandparent, while the blue nodes are the even valued grandparents.To solve this, we will follow these steps −Define a map called parentDefine a method called solve(), this will take node and parif node is null, then ... Read More

Binary Tree Coloring Game in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:47:13

539 Views

Suppose there are two players play a turn-based game on a binary tree. We have the root of this binary tree and the number of nodes n in the tree. Here n is odd, and each node has a distinct value from 1 to n. At first, the first player names a value x with 1

Implement Set Interface in JShell in Java 9

raja
Updated on 30-Apr-2020 11:44:59

193 Views

JShell is a command-line tool in Java 9 that has been used to execute simple statements like expressions, classes, interfaces, methods, and etc.A Set is an interface in Java that specifies a contract for collections having unique elements. If object1.equals(object2) returns true, then only one of object1 and object2 have a place in Set implementation.In the below code snippet, we have to use the Set.of() method. The collection returned by the Set.of() method is immutable, so it doesn't support the add() method. If we trying to add an element, throws UnsupportedOperationException. If we want to create a HashSet collection instead, which supports the ... Read More

Decrease Elements to Make Array Zigzag in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:43:20

416 Views

Suppose we have an array nums of integers, a move operation is actually choosing any element and decreasing it by 1. An array A is a zigzag array if either 1 or 2 is satisfied −Every even-indexed element is greater than adjacent elements, So. A[0] > A[1] < A[2] > A[3] < A[4] > ... and so on.Every odd-indexed element is greater than adjacent elements, So. A[0] < A[1] > A[2] < A[3] > A[4] < ... and so on.We have to find the minimum number of moves to transform the given array nums into a zigzag array.So if the ... Read More

Advertisements