Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 335 of 377

Find minimum number of currency notes and values that sum to given amount in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 2K+ Views

Suppose we have such amount, and we have to find the minimum number of notes of different denominations, that sum up to the given amount. Start from highest denomination notes, try to find as many notes possible for given amount. Here the assumption is that we have infinite amount of {2000, 500, 200, 100, 50, 20, 10, 5, 2, 1}. So if the amount is say 800, then notes will be 500, 200, 100.Here we will use the greedy approach to solve this problem.Example Live Demo#include using namespace std; void countNotes(int amount) {    int notes[10] = { 2000, 500, 200, ...

Read More

Find maximum in stack in O(1) without using additional stack in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 646 Views

Suppose we want to make a stack that can store the maximum element in the stack. And we can get it in O(1) time. The constraint is that, it should not use any additional space, so O(1) extra space.We can make one user-defined stack, that will store the max value, when one operation is performed, like pop or peek, then the max will be returned. For peek operation, return the maximum of stack top and the max element, for pop operation, when the top element is larger, then print it and update max as 2*max – top_element. otherwise return top_element. ...

Read More

Find k’th character of decrypted string in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 675 Views

Suppose we have one encoded string, where repetitions of substrings are represented as substring followed by count of substrings. So if the string is like ab2cd2, it indicates ababcdcd, and if k = 4, then it will return kth character, that is b here.To solve this, we initially take empty decrypted string then decompress the string by reading substring and its frequency one by one. Then append current substring in the decrypted string by its frequency. We will repeat this process till the string has exhausted, and print the Kth character from decrypted string.Example Live Demo#include using namespace std; char findKthCharacter(string ...

Read More

Find if a molecule can be formed from 3 atoms using their valence numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 143 Views

As we know the valance number is the number that defines how-many bonds the atom must form with other atoms. We have the valance numbers of three atoms. We have to check whether they can make one molecule or not. Atoms can form multiple bonds with each other. So if the valance numbers are 2, 4, 2, then the output will be YES. As the bonds are like below −1 – 2, 1 – 2, 2 – 3, 2 – 3.Suppose the valance numbers are a, b and c. Consider c is largest. Then we have two cases in which ...

Read More

Find i’th index character in a binary string obtained after n iterations in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 292 Views

Suppose we have a binary string bin. Then apply n iterations on it, and in each iteration 0 becomes 01 and 1 becomes 10, after that ith index character in the string after nth iteration. So if the binary string is 101, and n = 2, and i = 3, so after first iteration it will be 100110, in the next iteration, it will be 100101101001, so ith index is holding 1.To solve this, we have to follow these steps −Run loop n times, and in each iteration run another loop on the stringConvert each character of binary string, and ...

Read More

Find height of a special binary tree whose leaf nodes are connected in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 279 Views

Suppose we have a special binary tree, whose leaf nodes are connected to form a circular doubly linked list. We have to find its height. So the left pointer of the left most leaf will act as previous pointer of circular doubly linked list, and its right pointer will act as next pointer of the linked list.In this case the height finding strategy is similar to the normal binary search tree. We recursively calculate the height of the left and right subtrees of a node and assign height to the node is max of the two children + 1. But ...

Read More

Find coordinates of the triangle given midpoint of each side in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 243 Views

Suppose we have three coordinates which are midpoint of sides of the triangle. We have to find the coordinates of the triangle. So if the inputs are like (5, 3), (4, 4), (5, 5), then output will be (4, 2), (4, 6), (6, 4).To solve this, we have to solve for X-coordinates and Y-coordinates separately. For X coordinate of vertices, let them be x1, x2, x3. Then, X-coordinate of middle points will be (x1 + x2)/2, (x2 + x3)/2, (x3 + x1)/2. If we observe the sum of these three expressions is equal to sum of X-coordinates. Now, we have ...

Read More

Find closest number in array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 3K+ Views

Suppose we have an array A with n elements. And elements are sorted. We have to find the closest value to the given integer. The array may contain duplicate values and negative numbers. So if the array is like [2, 5, 6, 7, 8, 8, 9] and the target number is 4, then closest element is 5.We can solve this by traversing through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolute difference.Example Live Demo#include #include using namespace std; int getNearest(int x, int y, int target) { ...

Read More

Find the version of the Pandas and its dependencies in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Nov-2019 271 Views

Pandas is the important package for data analysis in Python. There are different versions available for Pandas. Due to some version mismatch, it may create some problems. So we need to find the version numbers of the Pandas. We can see them easily using the following code.We can use the command like below, to get the version −pandas.__version__Example>>> import pandas as pd >>> print(pd.__version__) 0.25.2 >>>We can also get the version of the dependencies using the function like below −pandas.show_versions() >>> pd.show_versions() INSTALLED VERSIONS ------------------ commit : None python : 3.7.1.final.0 python-bits : 64 OS : Windows OS-release : 7 ...

Read More

Find the smallest number X such that X! contains at least Y trailing zeros in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Nov-2019 145 Views

We have to take a number Y, we will find smallest number X, such that X! contains at least Y number of training zeros. For example, if Y = 2, then the value of X = 10. As X! = 3228800. It has Y number of zeros.We can solve this using binary search. The number of trailing zeros in N! is given by the count of the factors 5 in N!. X can be found using binary search in range [0, 5*Y]Example #include using namespace std; int factorCount(int n, int X) {    if (X < n)       ...

Read More
Showing 3341–3350 of 3,768 articles
« Prev 1 333 334 335 336 337 377 Next »
Advertisements