In C++, the scope defines the region within which a variable can be accessed. So, if broadly speaking, there are three main places where variables can be declared and accessed: Inside a function or a block, which are called local variables. In the definition of function parameters, which are called formal parameters. Outside of all functions, which are called global variables. Local Variables Local variables are the variables that are defined inside a function, method, or block of code within curly braces {}. These variables cannot be ... Read More
Manipulators in C++ are like helper functions that are designed to format and modify the input and output streams of your code. This is done by using the insertion () operators, and are defined in and header files. There are various types of manipulators that exist in C++, but in this article, we will be discussing the four main types, which are commonly used. endl setw setprecision setf C++ endl Manipulator The endl manipulator has the same functionality as ''(newline ... Read More
A histogram is a visual representation of the distribution of quantitively data. You are given a histogram represented by an array arr [], Where each of the array's elements represents the height of the bar in the histogram. All the bars have unit width, i.e., 1 unit. Your task is to determine the largest rectangle area possible in a given histogram, where the largest rectangle is made up of several consecutive bars with heights displayed in an array. Input / Output Scenario Let's understand the below input-output scenario with a diagram: Input: arr[] = [40, 10, 20, 50, 60, 30] ... Read More
To remove an item from a C++ STL vectors by value, you can use the std::remove inside the argument of erase method of the vector. There are some other methods as well. In this article, we will learn all the approaches to remove an item from a C++ STL vector. The easiest way to remove an item from a vector is to use the std::remove algorithm inside the erase method of the vector. For example: vector v = {1, 2, 2, 3, 2, 5}; // Remove all occurrences of 2 v.erase(remove(v.begin(), v.end(), 2), v.end()); Remove an ... Read More
In C++ STL, both emplace and insert functions are used to add elements to containers such as vectors, lists, and maps. But, emplace is more efficient than insert as it avoids unnecessary copying of object and does the insertion more efficiently than insert operation. In this article, we will discuss the all the differences between emplace and insert with examples. Insert in C++ STL The insert function in C++ STL is used to add new elements to a container (ie, set, map, vector, list, etc.). Following is the syntax of insert function: // insert into a pair multiset set1; ... Read More
Booth's algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. In this article, we have an array of multiplicand bits and an array of multiplier bits. Our task is to use Booth's algorithm to find the multiplication of these two binary numbers. Example of Booth's Algorithm In this example, we have used Booth's algorithm to calculate the multiplication of two signed binary numbers mathematically. Input: Multiplier(M): 01011 = 11 Multiplicand(Q): 01110 = 14 ... Read More
The quick sort technique is based on the partitioning of an array into smaller sub-arrays. It is based on the divide-and-conquer algorithm. The average time complexity of this algorithm is O(n*log(n)), but the worst complexity is O(n^2). To reduce the chances of the worst case, we implement the quick sort technique using randomization. In this article, we have an array having 6 elements. Our task is to sort the given array using the quick sort technique while avoiding the worst-case complexity. How to Implement Quick Sort with Given Complexity Constraint? In the ... Read More
Python provides different ways to find the execution time taken by a script or specific parts of the code such as using the functions from the time module, like time.time() or time.clock(). The following are some common methods used to measure execution time in Python: Using time.time() Function Using time.process_time() Function Using timeit Module Getting Program Execution Time Using time.time() Function The time.time() function returns the current time as a floating-point number that indicates the seconds elapsed since the epoch (when time began). To calculate the execution time ... Read More
Python provides various modules, such as time, datetime, and timeit, to measure time with high accuracy. These modules offer high-resolution clocks to measure time intervals. The following are several methods used to measure time with high precision in Python. Using time.time() Method Using time.perf_counter() Function Using timeit.default_timer() Using time.time() Method for Simple Timing The time.time() method returns the current time in seconds since the epoch as a floating-point number. The epoch is system-dependent, but on Unix-like systems, it is typically January 1, 1970, 00:00:00 (UTC). ... Read More
We can use the PyMongo library (the official Mongodb driver for Python) to connect to a Mongodb database and use it to insert, update, delete, etc objects. To include date and time information, Mongodb supports ISODate format, and PyMongo provides direct support for Python's datetime.datetime objects. There are multiple ways to prepare a Python date object for insertion into MongoDB, which we will discuss here: Create and Insert Date Object to MongoDB Using datetime.datetime.utcnow() The simplest way to create a Python date object that can be inserted into MongoDB is by using datetime.datetime.utcnow() from the datetime module. You can use ... Read More