In this article, we have an array of positive integers of size n. Our task is to calculate the maximum sum of triplet ( ai + aj + ak ) such that 0 sum = 12 3 6 10 => sum = 19 3 4 10 => sum = 17 4 5 10 => sum = 19 2 5 10 => sum = 17 Maximum sum = 19 Finding maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k]Here are the approaches ... Read More
In this article, we are given a mathematical series. Our task is to write a program to find the sum of the series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n. This can also be represented as: $$ 1+\displaystyle\sum\limits_{k=1}^n \left(\frac{x^k}{k}\right) $$ This series without starting 1 is known as the Taylor Expansion Series for -ln(1-x) where ln is the natural log. Example Here is an example of calculating the value of the given series: Input: x = 7, n = 4 Output: 747.08 ... Read More
In this article, we are given a mathematical series (1^1 + 2^2 + 3^3 + … + n^n) defined by a number n which defines the nth terms of the series. This series can be represented mathematically as: $$ \displaystyle\sum\limits_{k=1}^n k^k $$ The above series does not have any specific mathematical name but is generally referred to as the power tower series. Below is an example of the power tower series up to n. Example The following example calculates the sum of the given series 1^1 + 2^2 + 3^3 + … ... Read More
In this article, We are given an array arr[] of integers with length n. Our task is to count the number of triplets such that the sum of any two numbers is equal to the third number. Example Here is an example of counting the triplets whose sum of any two numbers equals the third one: Input: arr[]= {1, 2, 2, 3, 4} Output: 4 The explanation of the above example is as follows: Triplet 1: (1, 2, 3) => 1+2=3 Triplet 2: (1, 2, 3) => 1+2=3 Triplet ... Read More
What is Sparse Array?Sparse array is used to determines the dimension of an array in which most of the elements are zero. It can be used for matrix calculation. Characteristics of Sparse Array Following are list of points that defines the characteristics of a sparse array: Most of the elements are zero or null values which means it is unused. Only non-zero elements and their indexes are stored. It is used to save the memory while comparing to normal array calculation. Example The Sparse array of ... Read More
In C++, a superclass serves as a base/parent class. When we create a derived class (or child class) from a superclass, sometimes we have to call the constructor of the base class along with the constructor of the derived class. Unlike Java, there is no reference variable for the superclass. If the constructor is non-parameterized, then it will be called automatically with the derived class, otherwise, we have to put the superclass constructor in the initializer list of the derived class. Constructor Without Arguments It is a default constructor where no argument is passed to it. Here, we create a ... Read More
In C/C++, an inline function is a function where the compiler replaces the function call with the actual code of the function during compilation. So, this makes the program run faster than a normal function call. Why to Use Inline Function in C/C++? We should use an inline function in C/C++ when the function is very simple and small size. Also, avoid the regular function call and replace macros with type safety. Let us understand how an inline function is used for small functions. Suppose we write square(5), the compiler converts it directly to 5*5. This makes the program run ... Read More
Dynamic initialization of object refers to initializing the objects at run time i.e., the initial value of an object is to be provided during runtime. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors. This type of initialization is required to initialize the class variables during runtime. Why do we need the dynamic initialization? Dynamic initialization of objects is useful because It utilizes memory efficiently. Various initialization formats can be provided using overloaded constructors. It has the flexibility of using different formats ... Read More
In C++, we can read the integer values from text files using the ifstream class that allows the user to perform input operations from the files. Here, we have filename (tp.txt) that used in the program and store some integers value. 21 61 24 05 50 11 12 21 66 55 Example to Read Integers from a Text File In this example, we created one text file to save the integers values and then access those value using file handling operation. #include #include using namespace std; int main() { // Define the maximum ... Read More
In C++, the file size determines the total number of bytes. To get the file size, we can take reference of the fstream header which contains various functions such as in_file(), seekg(), and tellg(). Example Input: The file name is tp.txt content- "Hello Tutorialspoint" Output: Size of the file is 22 bytes Example Input: The file name is testfile.txt content- "Hello Tutorialspoint" Output: File size is 27 bytes Now, we will demonstrate the C++ programs of the above two examples using file handling. Getting File Size in C++ The short form of fstream header is file stream which ... Read More