Compute Index Using Pointers Returned by STL Functions in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:39:36

194 Views

In this section we will see how to generate index using pointers that are returned by STL in C++. As we know many inbuilt functions in C++ return the pointers to the position in memory which provides the address of desired number, but it has no relation with the actual index in the container of the returned value. As an example, to determine maximum element in a code, we use std::max_element() function, this does not return the index of the desired element, but return the address in memory. But sometimes, we need to get the index from that address. Here ... Read More

Check If a Number Is an Achilles Number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:38:56

295 Views

Suppose we have a number n; we have to check whether n is an Achilles number or not. As we know a number is Achilles number when a number that is powerful (A number N is called Powerful Number when for every prime factor p of it, p^2 also divides it) but not a perfect power. Some of the examples of Achilles numbers are: 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125.So, if the input is like 108, then the output will be True, as 6 and 36 both divide it and it is ... Read More

C++ Class with Dynamically Allocated Objects

Arnab Chakraborty
Updated on 27-Aug-2020 13:37:16

363 Views

In this problem we will see how we can make one class for which we can only create objects through dynamic memory allocation, no direct object creation is permitted.The idea is simple. We have to create private destructor for that class. When the destructor is private, the compiler would produce a compiler error for non-dynamically allocated objects because compiler need to remove them from stack segment once they are not available for use. For dynamically allocated objects, the programmer is responsible for deleting object, but the compiler is not responsible for that, so it permits creating objects dynamically.For avoiding memory ... Read More

Check If a Number is a Trojan Number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:35:01

160 Views

Suppose we have a number n, we have to check whether n is a Trojan Number or not. As we know that the Trojan Number is a number that is a strong number without a perfect power. A number n is a strong number when for every prime divisor or factor p of n, p^2 is also a divisor. In other words, every prime factor appears at least twice. Trojan numbers are strong. But the reverse is not true. So it means, not all strong numbers are Trojan numbers: only those that cannot be represented as a^b.So, if the input ... Read More

Object Storage When 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

Check if a Line at 45 Degree Can Divide the Plane into Two Equal Weight Parts in C++

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

155 Views

Suppose we have n different points (Xi, Yi) in 2D coordinate and each point has a weight Wi, we have to check whether a line at 45 degree can be drawn. So that the sum of weights of points on each side will be same.So, if the input is like[[-1, 1, 3], [-2, 1, 1], [1, -1, 4]], then the output will be True/To solve this, we will follow these steps −n := size of vDefine one map weight_at_xmax_x := -2000, min_x := 2000for initialize i := 0, when i < n, update (increase i by 1), do −temp_x := ... Read More

Check If a King Can Make a Valid Move on Modified Chessboard in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:26:52

415 Views

Suppose we have one infinite chessboard with the same rules as that of chess and if there are N knights coordinates on the infinite chessboard and the king’s coordinate, we have to check whether the King is checkmate or not. The coordinate of infinite board is bounded by large value like (-10^9

Restrictive Access in Derived Class Methods in C++

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

127 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

Check If a Key is Present in Every Segment of Size K in an Array in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:23:50

184 Views

Suppose we have an array A with N elements, we have another value p and a segment size k, we have to check whether key p is present in every segment of size k in A.So, if the input is like A = [4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4], p = 4 and k = 3, then the output will be TrueTo solve this, we will follow these steps −i := 0while i < n is non-zero, doj := 0while j < k, doif arr[j + i] is same as p, thenbreakj := j ... Read More

Check If a Given String is a Valid Number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:20:44

764 Views

Suppose we have a string, that holds numeric characters and decimal points, we have to check whether that string is representing a number or not. If the input is like “2.5”, output will be true, if the input is “xyz”, output will be false.To solve this, we will follow these steps −To solve this, we will use the string parsing technique of our programming language. We will try to convert string to number, if there is no exception, then that will be a number, otherwise not a number.ExampleLet us see the following implementation to get better understanding − Live Demodef isNumeric(s): ... Read More

Advertisements