Found 7334 Articles for C++

C++ Program to Find Sum of Leaf Node in Binary Tree

AYUSH MISHRA
Updated on 28-Apr-2025 21:36:08

5 Views

We can find the sum of leaf nodes in a binary tree using an iterative and a recursive approach. This problem has many real-life applications, such as analyzing hierarchies, calculating final results in decision trees, or summing final nodes in various kinds of trees used in computer algorithms. In this article, we are going to learn how can find the sum of all leaf nodes in a binary tree in the C++ language. What are Leaf Nodes in a Binary Tree? The leaf nodes of a binary tree are the nodes that do not have any children. Both the ... Read More

C++ Program to Split a Circular Linked List into Two Halves

AYUSH MISHRA
Updated on 24-Apr-2025 10:41:46

103 Views

A circular linked list is a type of linked list where the last node and first node are the same, forming a loop. There are many real-life applications of splitting a circular linked list into two halves for easier processing, load balancing, or efficient data manipulation. In this article, we are going to learn different approaches to split a circular linked list into two halves in C++. What is the Splitting of a Circular Linked List? Splitting of a Circular linked list is dividing the given linked list into two separate linked lists: If ... Read More

Kth largest Element in a 2D matrix where each row and column is sorted in C++

AYUSH MISHRA
Updated on 24-Apr-2025 10:33:13

9 Views

Finding the Kth largest element in a 2D matrix where each row and column is sorted is a common problem in computer science. This problem has many real-life applications, such as a ranking system, where we need to find the Kth highest score in a sorted dataset. In this article, we are going to discuss how we can find the Kth largest element in a 2D matrix e where each row and column is in sorted order in the C++ language. What is the Kth Largest Element? The Kth largest element refers to an element that is placed at ... Read More

Swap all odd and even bits using C++

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

20 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

45 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

246 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

75 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

45 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

46 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

28 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

1 2 3 4 5 ... 734 Next
Advertisements