Suppose we want to place 1, 2, 3, 4, 5, 6, 7, 8, into the eight circles in the given figure, in this way that no number is adjacent to a number that is next to it in the sequence.So, if the input is like0-1-10-1-1-1-10-1-10then the output will beTo solve this, we will follow these steps −N := 3, M := 4NOTCONSIDERED := -1Define a function present_in_grid(), this will take grid[N][M], num, for initialize i := 0, when i < N, update (increase i by 1), do:for initialize j := 0, when j < M, update (increase j by 1), ... Read More
Here we will see the map container and its use in C++. The maps are defined as associative containers that store elements in a hash-mapped fashion. Each element is associated with a key and value. No two mapped values can have identical keys. These are some fundamental methods that are present inside the map container in C++.begin(): This returns an iterator to the first element in the map.end()− This returns an iterator to the theoretical element that follows last element in the map.size() − This returns the number of elements in the map.max_size() − This returns the maximum number of ... Read More
In this section we will see the heap data structure present in C++ STL. This permits faster input into heap and retrieval of a number always results in the largest number i.e. largest number of the remaining numbers is popped out each time. Other elements of the heap are arranged which depends on the implementation. The heap operations are as follows −make_heap() − This converts a range in a container to a heap.front() − This returns first element of heap which is the largest number.ExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; int ... Read More
In this section, we will see how to make point class using complex class from STL in C++. And apply them on some geometry related problems. The complex number is present inside the complex class from STL (#include )Defining Point ClassTo make complex to point, we will change the name of the complex as point, then change x to real() of complex class and y to imag() of complex class. Thus, we can simulate the point class.# include typedef complex point; # define x real() # define y imag()We have to keep in mind that the x and y ... Read More
In this section we will see how we can use C++ STL function to generate test cases. Sometimes generating test cases for array programs can be very complicated and inefficient process. C++ provides two methods to generate test cases. These methods are as follows −The generate() methodThe C++ function std::algorithm::generate() assigns the value returned by successive calls to gen to the elements in the range of first to last. It takes three parameters first, last and gen, these are forward iterator to the initial position, backward iterator to the final position and generator function that is called with no argument, ... Read More
Suppose we have a number n, we have to check whether n is a primorial prime or not. A number is said to be a primorial prime when it is a prime number of the form pN# + 1 or pN# – 1 , where pN# indicates the primorial of pN such that the product of first N prime numbers.So, if the input is like 29, then the output will be True as 29 is Primorial prime of the form pN - 1 if N=3, Primorial is 2*3*5 = 30 and 30-1 = 29.To solve this, we will follow these ... Read More
As we know the Gaussian Filtering is very much useful applied in the field of image processing. It is used to reduce the noise of an image. In this section we will see how to generate a 2D Gaussian Kernel. Gaussian Distribution for generating 2D kernel is as follows.$$G(x,y)= \frac{1}{2\Pi\:\sigma^{2}}e^{\frac{x^{2}+y^{2}}{2\sigma^{2}}}$$ExampleLet us see the following implementation to get better understanding − Live Demo#include #include #include #define PI 3.1415 using namespace std; void calc_filter(double kernel[][5]) { double sigma = 1.0; double p, q = 2.0 * sigma * sigma; double sum = 0.0; for (int x = -2; x
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
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
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