C++ Articles - Page 298 of 719

Where is an object stored if it is created inside a block in C++?

Arnab Chakraborty
Updated on 27-Aug-2020 13:30:13

1K+ Views

In this section we will discuss about where the variables and objects are stored in the memory when a C++ program is compiled. As we know, there are two parts of memory in which an object can be stored −Stack − All members that are declared inside block of memory, it stores inside the stack section. The main function is also a function, so elements inside it will be stored inside the stack.Heap − When some objects are allocated dynamically, then that is stored inside the heap section.The scope of the object declared inside a block or a function is ... Read More

What happens when more restrictive access is given to a derived class method in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:25:03

125 Views

In this section we will discuss about interesting facts restrictive access of derived class methods in C++. We will see some examples and analyze the output to know more about restrictions of using derived class methods in C++.Example (C++)Let us see the following implementation to get better understanding −#include using namespace std; class BaseClass { public:    virtual void display(){       cout

What happens when a virtual function is called inside a non-virtual function in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:43:40

331 Views

In this section we will discuss about interesting facts about virtual classes in C++. We will see two cases first, then we will analyze the fact.At first execute the program without using any virtual function.The execute the program using any virtual function under non-virtual function.ExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; class BaseClass { public:    void display(){       cout

Find the value of the function Y = (X^6 + X^2 + 9894845) % 981 in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:34:32

118 Views

Suppose we have given function like f(x) = (x^6 + x^2 + 9894845) % 971, now for a given value of x, we have to find the value of f(x).So, if the input is like 5, then the output will be 469To solve this, we will follow these steps −Define a function power_mod(), this will take base, exponent, modulus, base := base mod modulusresult := 1while exponent > 0, do −if exponent is odd, then −result := (result * base) mod modulusbase := (base * base) mod modulusexponent = exponent /2return resultFrom the main method do the following −return power_mod(n, ... Read More

Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L) in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:11:48

204 Views

Suppose we have a string, s, and we have another list with few words, these words are of same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “wordgoodgoodgoodword” and words are ["word", "good"], then the output will be [0, 12]. This is because the substring starting at index 0 and 12 are “wordgood” and “goodword”.To solve this, we will follow these steps −Define a method called ok(), this will take string s, map wordCnt and ... Read More

Find the shortest distance between any pair of two different good nodes in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:03:08

478 Views

Suppose we have a given weighted undirected graph with N different nodes and M edges, some of the nodes are good nodes. We have to find the shortest distance between any pair of two different good nodes. In the given diagram the yellow in the following graph are considered to be good nodes.So, if the input is likethen the output will be 11, as the pairs of good nodes and distance between them are: (1 to 3) the distance is 11, (3 to 5) the distance is 13, (1 to 5) the distance is 24, out of which 11 is ... Read More

Find the probability of reaching all points after N moves from point N in C++

Arnab Chakraborty
Updated on 27-Aug-2020 11:58:22

140 Views

Suppose we have a number N this represents the initial position of the person on the number line. We also have L which is the probability of the person of going left. We have to find the the probability of reaching all points on the number line after completing N moves from point N. Each move can be either to the left or to the right.So, if the input is like n = 2, l = 0.5, then the output will be [0.25, 0, 0.5, 0, 0.25]To solve this, we will follow these steps −high := 1 - lowDefine an ... Read More

Find the minimum number of rectangles left after inserting one into another in C++

Arnab Chakraborty
Updated on 20-Aug-2020 08:28:52

229 Views

Suppose we have width and height of N different rectangles; we have to find the minimum number of rectangles left after inserting one into another. So, if W1 and W2 be the width of rectangles R1 and R2 respectively. And H1 and H2 be the height of R1 and R2 respectively, then if W1 < W2 and H1 < H2 then rectangle R1 fits inside rectangle R2. Thus, the smallest one can fit into second smallest, then that fits into the next and so on.So, if the input is like {{ 30, 45 }, { 15, 15 }, { 45, ... Read More

Find the maximum cost of an array of pairs choosing at most K pairs in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:53:46

210 Views

Suppose we have an array of pairs A; we have to find the maximum cost for selecting at most K pairs. In this case, the cost of an array of pairs type elements is the product of the sum of first elements of the selected pair and the smallest among the second elements of the selected pairs. As an example, if these pairs are selected (4, 8), (10, 3) and (3, 6), then the cost will be (4+10+3)*(3) = 51, for K=3So, if the input is like A = [(15, 5), (65, 25), (35, 20), (20, 5), (35, 20), (15, ... Read More

Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array in C++

Arnab Chakraborty
Updated on 20-Aug-2020 07:42:47

453 Views

Suppose we have two arrays A and B with n numbers, we have to rearrange the elements of B in itself in a way such that the sequence formed by (A[i] + B[i]) % n after rearranging it is lexicographically smallest. Finally we will return the lexicographically smallest possible sequence.So, if the input is like A = {1, 2, 3, 2}, B = {4, 3, 2, 2}, then the output will be [0, 0, 1, 2]To solve this, we will follow these steps −n := size of aDefine one map my_mapDefine one set my_setfor initialize i := 0, when i ... Read More

Advertisements