Find Profession in a Special Family in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:14:18

196 Views

Consider there is a special family of doctors and engineers. There are some rules, these are like below −Everybody has two childrenFirst child of an engineer is an engineer, second child is doctorFirst child of a doctor is a doctor, second child is an engineerAll generations of doctors and engineers starts with engineerSo if we want to get result for level 4 and pos 2, then result will be DoctorThe idea is simple. Profession of a person depends on following two.Profession of the parent.Position of node: When the position of a node is odd, then its profession is same as ... Read More

Find Minimum Number of Currency Notes in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:11:27

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

JavaScript Symbol Description Property

AmitDiwan
Updated on 17-Dec-2019 11:03:49

120 Views

The symbol.description property in JavaScript converts a Symbol object to a primitive value. The syntax is as follows −Symbol()[Symbol.toPrimitive](hint);Example Live Demo Demo Heading Click to display... Result    function display() {       const val = Symbol('john');       console.log(val[Symbol.toPrimitive]);    } OutputClick the “Result” button −Example Live Demo Demo Heading Click to display... Result    function display() {       const val = Symbol(2465);       var res = val[Symbol.toPrimitive](99);       console.log(res)    } OutputClick the “Result” button and get the result in Console −

Find Maximum in Stack in O(1) Without Using Additional Stack in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:02:20

620 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

JavaScript Array Prototype Values

AmitDiwan
Updated on 17-Dec-2019 10:58:48

188 Views

The array.values() method of JavaScript returns a new Array Iterator object that contains the values for each index in the array.The syntax is as follows −arr.values()Let us now implement the array.values() method in JavaScript −Example Live Demo Demo Heading Click the button to display the value... Result    function display() {       var arr = ['p', 'q', 'r'];       var res = arr.values();       document.getElementById("test").innerHTML = res.next().value    } OutputClick on the “Result” button −Example Live Demo Ranking Points Click to display the points... Result   ... Read More

Find K-th Character of Decrypted String in C++

Arnab Chakraborty
Updated on 17-Dec-2019 10:56:25

640 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

JavaScript toLocaleString Function

AmitDiwan
Updated on 17-Dec-2019 10:55:12

270 Views

The toLocaleString() method of JavaScript is used to convert a Date object to a string using locale settings.The syntax is as follows −Date.toLocaleString(locales, options)Above, the locales parameter is the language specific format to be used −ar-SA Arabic (Saudi Arabia) bn-BD Bangla (Bangladesh) bn-IN Bangla (India) cs-CZ Czech (Czech Republic) da-DK Danish (Denmark) de-AT Austrian German de-CH "Swiss" German de-DE Standard German (as spoken in Germany) el-GR Modern Greek en-AU Australian English en-CA Canadian English en-GB British English en-IE Irish English en-IN Indian English en-NZ New Zealand English en-US US English en-ZA English (South Africa) es-AR Argentine Spanish es-CL Chilean Spanish ... Read More

Determine Molecule Formation from Atoms' Valence Numbers in C++

Arnab Chakraborty
Updated on 17-Dec-2019 10:51:32

115 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

JavaScript Array Splice Method

AmitDiwan
Updated on 17-Dec-2019 10:50:17

234 Views

The splice() method of JavaScript is used to add or remove item. It returns the removed item.The syntax is as follows −array.splice(index, num, item1, ....., itemX)Here, index is the integer specifying at what position to add or remove items, num is the number of items to remove, item1…itemX are the items to be added to the array.Let us now implement the splice() method in JavaScript −Example Live Demo Products Click to display the updated product list... Result    var products = ["Electronics", "Books", "Accessories"];    document.getElementById("test").innerHTML = products;    function display() {       products.splice(1, 2, ... Read More

Find i-th Index Character in Binary String After n Iterations in C++

Arnab Chakraborty
Updated on 17-Dec-2019 10:46:00

255 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

Advertisements