Found 7197 Articles for C++

Find profession in a special family in C++

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

190 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 and values that sum to given amount 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

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

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

609 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
Updated on 17-Dec-2019 10:56:25

623 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
Updated on 17-Dec-2019 10:51:32

104 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
Updated on 17-Dec-2019 10:46:00

244 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
Updated on 17-Dec-2019 10:40:20

241 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
Updated on 17-Dec-2019 10:19:20

196 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
Updated on 17-Dec-2019 10:16:42

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

C++ Program to implement t-test

Ayush Gupta
Updated on 03-Dec-2019 11:26:46

961 Views

In this tutorial, we will be discussing a program to implement t-test.The t-test of the student’s T test is used to compare two means and tell if both of them are similar or different. Along with this, t-test also helps to determine how large the differences are to know the reason for the change.Example#include using namespace std; //calculating mean float calc_mean(float arr[], int n){    float sum = 0;    for (int i = 0; i < n; i++)       sum = sum + arr[i];    return sum / n; } //calculating standard deviation float calc_deviation(float arr[], ... Read More

Advertisements