Coin Change Problem in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:50:21

5K+ Views

Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1, 2, 5], and the amount is 11, the output is 3. This is formed using 5 + 5 + 1 = 11.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then ... Read More

Sum Root to Leaf Numbers in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:45:08

473 Views

Suppose we have a binary tree containing digits from 0-9 only, here all root-to-leaf path could represent a number.So if the tree is like −This is representing two paths 21 and 23, so the output will be 21 + 23 = 44.To solve this, we will follow these steps −Create one recursive function called dfs(), this will take root, and num. initially num = 0if the node is not nullnum := num * 10 + value of nodeif node right is not null and node left is not null, then’sum := sum + numnum := num / 10return from the ... Read More

Importance of jdeps Tool in Java 9

raja
Updated on 28-Apr-2020 11:33:16

528 Views

The jdeps is a Java Class Dependency Analyzer tool, which is a command-line tool to show the package-level or class-level dependencies of given Java class files. The input classes can be given as a path-name to a .class file, a directory, a jar file, or it will be a fully qualified class name to analyze all class files."jdeps" has included in the jdk installation since jdk 8 and, it is represented by the "%java_home%\bin\jdeps.exe" program file. If we have "%java_home%\bin" directory included in the "path" environment variable, we will run "jdeps --help" command to see a complete list of all options. Below, we can ... Read More

Unique Binary Search Trees II in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:21:10

470 Views

Suppose we have an integer n, we have to generate all structurally unique binary search trees that store values from 1 to n. So if the input is 3, then the trees will be −To solve this, we will follow these steps −Define one recursive function called generate(), this will take low and highdefine one tree node called temp.if low > high, then insert null into the temp, and return tempfor i in range low to highleft_subtree := generate(low, i – 1)right_subtree := generate(i + 1, high)current := ifor j in range 0 to size of the left_subtreefor k in ... Read More

Missing Number in Arithmetic Progression Using C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:13:44

362 Views

Suppose we have an array that represents elements of arithmetic progression in order. One element is missing. We have to find the missing element. So if arr = [2, 4, 8, 10, 12, 14], output is 6, as 6 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the difference between the middle and next to the middle is the same as diff or not. If not, then the missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the ... Read More

N-th Tribonacci Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:07:03

436 Views

Suppose we have a value n, we have to generate n-th Tribonacci number. The Tribonacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding three previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few numbers to start, are {0, 1, 1}We can solve them by following this algorithm −Algorithm• first := 0, second := 1, third := 1 • for i in range n – 3, do    o next := first ... Read More

Hamming Distance in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:05:39

4K+ Views

Consider we have two integers. We have to find the Hamming distance of them. The hamming distance is the number of bit different bit count between two numbers. So if the numbers are 7 and 15, they are 0111 and 1111 in binary, here the MSb is different, so the Hamming distance is 1.To solve this, we will follow these steps −For i = 31 down to 0b1 = right shift of x (i AND 1 time)b2 = right shift of y (i AND 1 time)if b1 = b2, then answer := answer + 0, otherwise answer := answer + ... Read More

Fizz Buzz in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:03:59

4K+ Views

Suppose we have a number n. We have to display a string representation of all numbers from 1 to n, but there are some constraints.If the number is divisible by 3, write Fizz instead of the numberIf the number is divisible by 5, write Buzz instead of the numberIf the number is divisible by 3 and 5 both, write FizzBuzz instead of the numberTo solve this, we will follow these steps −For all number from 1 to n, if a number is divisible by 3 and 5 both, print “FizzBuzz”otherwise when the number is divisible by 3, print “Fizz”otherwise when ... Read More

First Unique Character in a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:01:32

4K+ Views

Suppose we have a string and we have to find the first unique character in the string. So if the string is like “people”, the first letter whose occurrence is one is ‘o’. So the index will be returned, that is 2 here. If there is no such character, then return -1.To solve this, we will follow these steps −create one frequency mapfor each character c in the string, doif c is not in frequency, then insert it into frequency, and put value 1otherwise, increase the count in frequencyScan the frequency map, if the value of a specific key is ... Read More

Sum of Two Integers in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:58:33

855 Views

Suppose we have two integers a and b. Our task is to find the sum of these two integers. One constraint is that, we cannot use any operator like + or -. So if a = 5 and b = 7, the result will be 12.To solve this, we will follow these steps −For solving we will use the bitwise logical operatorsIf b = 0, then return aotherwise, recursively use the sum function by providing an XOR b, and a AND b after left shifting the result one timeExample (Python)Let us see the following implementation to get a better understanding ... Read More

Advertisements