Found 7365 Articles for C++

19 Views
We can swap all odd and even bits in a given integer. Swapping of odd and even bits means changing the bits present at odd positions with the bits at even positions in a binary representation of a number. One real-life application is optimizing data storage or modifying data patterns in memory. In this article, we are going to learn how we can swap all odd and even bits of a number in C++ using different approaches. Formula for Swapping Odd and Even Bits Extract all even-positioned bits using a bit mask and shift ... Read More

44 Views
In this problem, we are given a binary string, and we have to check if all the 1s present in the binary string are at an equivalent distance from each other. Example 1 Input: binaryString = "10101010" Output: Yes Explanation The positions of 1s in the given string are 1, 3, 5, and 7.Here, the difference between consecutive 1s is 2 (as 7-5 equals 5-3 equals 3-1), so all 1s are at an equivalent distance from each other. Example 2 Input: binaryString = ... Read More

41 Views
What is Harmonic Progression? A Harmonic Progression is a sequence of numbers formed by the reciprocal of an Arithmetic progression sequence. If a given sequence a1, a2, a3, a4... is in Arithmetic progression, then 1/a1, 1/a2, 1/a3, 1/a4... forms a Harmonic Progression. Formula for the nth term of Harmonic Progression? The formula for the nth term of a Harmonic Progression is: Tn = 1 / (1/a1 + (n - 1) * d) where: a1 is the first term of the Harmonic Progression. d is the common difference of the corresponding Arithmetic ... Read More

73 Views
In this article, we will learn how to initialize a std::multiset with a custom comparator in C++. A multiset is similar to a set, except that it allows duplicate elements. It stores elements in a sorted order based on a comparison function. By default, elements are compared using the < (less-than) operator. However, with a custom comparator, we can define how elements should be compared. For example, if we define a custom comparator that sorts elements in descending order and insert the following elements into the multiset: Input: 3 12 5 8 1 7 9 4 6 Output: {12, 9, ... Read More

44 Views
In this article, we will learn how to pass an array into a lambda function in C++. A lambda function is a small, anonymous function that doesn't have a name and can access variables from the surrounding code. Passing an array to a lambda function is a bit different from passing a single value because you need to define how the array will be handled inside the lambda. Let's see how to do that. Passing an Array into a Lambda Function We will cover four main methods to pass arrays into lambda functions in C++: ... Read More

45 Views
In C++, a name conflict occurs when two or more identifiers, such as variables, functions, or classes, share the same name within the same scope. This creates ambiguity because the compiler may struggle to distinguish between them, leading to compilation errors. In this article, we will discuss name conflicts in C++ and also ways to resolve them, ensuring your code remains clear and error-free. Name Conflict Error in C++ Name conflicts typically occur when the same name is used for different variables, functions, or classes in conflicting or overlapping scopes, or when they are declared globally or within the ... Read More

26 Views
In this article, we'll discuss the problem of finding the longest prefix in a string that has the highest frequency (appears most often) across the string. We'll break it down step-by-step and show how to efficiently solve this problem. Given a string, we need to find the longest prefix (the starting sequence) that appears most often in the string. A prefix is simply a substring that begins at the start of the string and includes one or more characters. Let's understand with an example. For the string 'abcabc', the prefixes are: a ... Read More

26 Views
In this article, we will learn how to reduce a given array by replacing all subarrays with values less than a given number K by their sum. This process simplifies the array by merging smaller values. Given an array of integers and a value K, we need to find all contiguous subarrays whose values are less than K and replace them with their sum. After performing this reduction, the resulting array will have fewer elements, as the smaller subarrays will be combined into one. Let's consider an example. Given the array: [1, 2, 3, 5, 1, 4, 6, ... Read More

1K+ Views
There are a total of 365 days in a normal year and 366 days in a leap year. In this article, we are going to learn how we can find the number of days in a month of a given particular year. Here, we will learn about different approaches for calculating the number of days in a given month using C++. How to Find the Number of Days in a Given Month? There are a total of 12 months in a year. The number of days in a given month from 1 to 12 in a given leap or non-leap ... Read More

108 Views
In this article, we will learn how to deallocate a 2D array in C++. A 2D array is an array of arrays, where each element points to another array. When you dynamically allocate a 2D array, memory is reserved for each row and the main array on the heap. Deallocating memory means freeing the memory that was previously allocated for the 2D array so it can be reused. In C++, this involves deleting each row first and then deleting the main array. Deallocating a 2D Array in C++ In C++, a 2D array can be dynamically allocated in ... Read More