Server Side Programming Articles - Page 2010 of 2650

Find the count of maximum contiguous Even numbers in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:37:01

351 Views

Suppose we have an array A with n elements. We have to find the maximum number of the contiguous even numbers in the given array. So if the array is like A = [1, 2, 3, 4, 6, 8, 7], then the count will be 3.We can solve this easily. We need two count variables one is max_current, and another is max_till_now. If an even number is found, then increase max_current, then compare it with max_till_now. Every time an odd element is found reset max_count to 0.Example Live Demo#include using namespace std; int maxEvenContiguous(int arr[], int n) {    int max_current ... Read More

Find the area of largest circle inscribed in ellipse in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:23:47

170 Views

Suppose we have an ellipse, with major and minor axis length 2a & 2b. We have to find the area of the largest circle that can be inscribed in it. So if the a = 5 and b = 3, then area will be 28.2734From the we can see that the radius of the maximum area circle inscribed in an ellipse will be the minor axis ‘b’. So the area will be A = π*b*bExample Live Demo#include using namespace std; double inscribedCircleArea(double b) {    double area = 3.1415 * b * b;    return area; } int main() {    double a = 10, b = 8;    cout

Find sum of all nodes of the given perfect binary tree in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:21:48

222 Views

Suppose we have a positive integer L, which represents the number of levels in a perfect binary tree. The leaf nodes in this perfect binary tree are numbered starting from 1 to n. Where the n is number of leaf nodes. The parent node is the sum of children. Our task is to write a program to print the sum of all of the nodes of this perfect binary tree. So if the tree is like below −So total sum is 30.If we see closer, we need to find the sum of all of the nodes. As the leaf nodes ... Read More

Find relative complement of two sorted arrays in C++

Arnab Chakraborty
Updated on 17-Dec-2019 11:16:34

454 Views

Suppose we have two sorted arrays arr1 and arr2, there sizes are m and n respectively. We have to find relative complement of two arrays. It means that we need to find all those elements which are present in arr1, but not in arr2. So if the arrays are like A = [3, 6, 10, 12, 15], and B = [1, 3, 5, 10, 16], then result will be [6, 12, 15]To solve this, we can use the set_difference function. As the problem is basically set difference operation.Example Live Demo#include #include #include using namespace std; int main() {    int first[] ... Read More

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 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

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

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

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

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

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

257 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