Found 7365 Articles for C++

Swap all odd and even bits using C++

AYUSH MISHRA
Updated on 22-Apr-2025 15:11:28

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

Check if all the 1s in a binary string are equidistant or not in C++

AYUSH MISHRA
Updated on 14-Apr-2025 12:40:11

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

C++ program for finding nth term of H.P.

AYUSH MISHRA
Updated on 09-Apr-2025 18:56:09

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

How to initialize multi-set with custom comparator in C++

Nishu Kumari
Updated on 27-Mar-2025 14:47:12

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

How to pass an array into a lambda function in C++

Nishu Kumari
Updated on 27-Mar-2025 14:55:25

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

How to resolve a name conflict in C++

Nishu Kumari
Updated on 27-Mar-2025 14:58:44

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

Find the longest prefix in a string that has the highest frequency across the string.

Nishu Kumari
Updated on 27-Mar-2025 15:02:49

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

Reduce a given array by replacing all subarrays with values less than a given number K by their sum

Nishu Kumari
Updated on 27-Mar-2025 15:13:53

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

C++ Program to count the number of days in a given month of a year

AYUSH MISHRA
Updated on 27-Mar-2025 14:35:49

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

How to deallocate a 2D array in C++

Nishu Kumari
Updated on 08-Apr-2025 11:15:34

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

Advertisements
close