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
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
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
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
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
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
Try/Catch block in PowerShell is to handle the errors which are produced in the script. To be specific, the errors should be terminating errors. The Finally block in the PowerShell is not mandatory to write each time along with Try/Catch but it will be executed regardless the error occurs or not.So when you use the Try block, the Catch block is mandatory but not Finally block.Try/Catch block with Terminating error − Below is the example of Terminating error without finally block.Exampletry{ This is not allowed "This is Allowed" } catch{ Write-Host "Error occured" -BackgroundColor DarkRed }OutputPS C:\WINDOWS\system32> ... Read More
We are given with a number let’s say, num and the task is to calculate the divisor of a given number thereby count the divisors of num that have at least one digit common with n.Input − num = 24Output − Count is 4Explanation − we will perform the following steps −Firstly, calculate the divisors of a given numberDivisors of 24 are − 1, 2, 3, 4, 6, 8, 12, 24Secondly, check which divisor have at least one digit that matches with the digits of a number2, 4, 12, 24 are the divisors that contain the digit that matches with ... Read More
We are given a natural number let’s say, num and the task is to calculate the count of all those natural numbers whose all permutations are greater than that number.We are working with the following conditions −The data should be natural numbers onlyAll the possible permutations or arrangement of a natural number should be equal or greater than the given number. For example, the number is 20Consider all the numbers till 20 starting from 1 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20Now check those numbers whose arrangement or permutation is equaled or greater than the given number i.e. 20. Numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11=11, 12
We are given a total number of heads and legs in a zoo and the task is to calculate the total number of animals there in the zoo with the given data. In the below program we are considering animals to be deer and peacocks.Input −heads = 60 legs = 200Output −Count of deers are: 40 Count of peacocks are: 20Explanation −let total number of deers to be : x Let total number of peacocks to be : y As head can be only one so first equation will be : x + y = 60 And deers have 4 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP