Making a Large Island in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:28:50

299 Views

Suppose we have a 2D grid of binary values (0s and 1s), we change at most one 0 to a 1. After that we have to find what is the size of the largest island? Here an island is a 4-directionally (top, bottom, left, right) connected group of 1s.So, if the input is like [[1, 0], [0, 1]], then the output will be 3, this is because if we change one 0 to 1 and connect two 1s, then we will get an island with area = 3.To solve this, we will follow these steps −Define an array dir of ... Read More

Bricks Falling When Hit in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:26:54

242 Views

Suppose we have a grid of binary values (0s and 1s) the 1s in a cell represent the bricks. A brick will not drop when that satisfies these conditions −Either brick is directly connected to the top of the gridor at least one of its adjacent (top, bottom, left, right) bricks will not drop.We will do some erasures sequentially. In each case we want to do the erasure at the location (i, j), the brick (if that is present) on that location will disappear, and then some other bricks may drop because of that erasure. We have to find the ... Read More

Sliding Puzzle in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:23:31

2K+ Views

Suppose we have one 2x3 board, there are 5 tiles those are represented by the numbers 1 through 5, and one empty square is there, that is represented by 0.Here a move means 0 and one adjacent number (top, bottom, left or right) and swapping it. This will be solved when the elements are arranged in this manner: [[1, 2, 3], [4, 5, 0]].We have the puzzle board; we have to find the least number of moves required so that the state of the board is solved. If this is not possible to solve, then return -1.So, if the input ... Read More

Max Chunks to Make Sorted II in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:19:39

273 Views

Suppose we have an array arr of integers, we have to split the array into some number of partitions, and individually sort each partition. Now after concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made?So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5].To solve this, we will follow these steps −cnt := 1n := size of arrDefine an array maxOfLeft of size nDefine an array minOfRight of size nmaxOfLeft[0] ... Read More

Couples Holding Hands in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:17:43

430 Views

Suppose there are N couples and they have sat on 2N seats arranged in a row and want to hold hands. We have to find the minimum number of swaps so that every couple is sitting side by side.The people and seats are represented by a number from 0 to 2N-1, the couples are numbered in order, this is like the first couple as (0, 1), the second couple as (2, 3), and so on and the last couple as (2N-2, 2N-1).The couples' initial seating is given by another array called row, and row[i] being the value of the person ... Read More

Special Binary String in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:15:14

2K+ Views

Suppose we have a spatial binary string. This string has following few properties −There are same number of 0s and 1sEvery Prefix in the binary string has at least as many 1s as 0sNow suppose we have special string S, a move is actually choosing two consecutive, non-empty, special substrings of S, and swapping them.We have to find the lexicographically largest resulting string possible, at the end of any number of moves.So, if the input is like 11011000, then the output will be 11100100, this is because: The substrings "10" and "1100" are swapped. This is the lexicographically largest string ... Read More

Number of Atoms in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:12:02

585 Views

Suppose we have a chemical formula; we have to find the count of each atom.An atomic element will always start with an uppercase character, there can be zero or more lowercase letters, representing the name. And 1 or more digits representing the count of that element may follow if the count is greater than 1. But if the count is 1, no digits will follow. As an example, H2O and H2O2 both are valid, but H1O2 is invalid.So, if the input is like Na2(CO)3, then the output will be C3Na2O3, so this indicates 3 Carbon (C), 2 Sodium (Na), 3 ... Read More

Advanced Master Theorem for Divide and Conquer Recurrences

sudhir sharma
Updated on 08-Jun-2020 05:44:13

3K+ Views

Divide and conquer is an algorithm that works on the paradigm based on recursively branching problem into multiple sub-problems of similar type that can be solved easily.ExampleLet’s take an example to learn more about the divide and conquer technique −function recursive(input x size n)    if(n < k)       Divide the input into m subproblems of size n/p.       and call f recursively of each sub problem    else       Solve x and returnCombine the results of all subproblems and return the solution to the original problem.Explanation − In the above problem, the problem ... Read More

Measure Elapsed Time in Python

Rajendra Dharmkar
Updated on 07-Jun-2020 17:37:24

2K+ Views

To measure time elapsed during program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1) # CPU seconds elapsed (floating point)OutputThis will give the output −Time elapsed:  1.2999999999999123e-05You can also use the time module to get proper statistical analysis of a code snippet's execution time.  It runs the snippet multiple times and then it tells you how long the shortest run took. You can use it as follows:Exampledef f(x):   return x * x ... Read More

Difference Between BLOB and CLOB Data Types

Krantik Chavan
Updated on 07-Jun-2020 07:10:46

15K+ Views

Blob and Clob together are known as LOB(Large Object Type). The following are the major differences between Blob and Clob data types.BlobClobThe full form of Blob is a Binary Large Object.The full form of Clob is Character Large Object.This is used to store large binary data.This is used to store large textual data.This stores values in the form of binary streams.This stores values in the form of character streams.Using this you can stores files like videos, images, gifs, and audio files.Using this you can store files like text files, PDF documents, word documents etc.MySQL supports this with the following datatypes:TINYBLOBBLOBMEDIUMBLOBLONGBLOBMySQL ... Read More

Advertisements