Found 7333 Articles for C++

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

29 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

27 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

110 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

How to find the greatest divisor that divides all the natural numbers in a given range [L, R]

Nishu Kumari
Updated on 24-Mar-2025 15:20:37

36 Views

In this article, we will explain how to find the greatest divisor that divides all the natural numbers in a given range [L, R]. The task is to identify the largest number that can divide every number between L and R, including both L and R. For example, in the range [6, 9] (which includes 6, 7, 8, and 9), the GCD is 1 because no number greater than 1 divides all of them. Now, let's go through the steps to solve this problem. Approach to Solve the Problem To solve this problem, we can follow these steps: ... Read More

C++ program for mirror of matrix across diagonal

Nishu Kumari
Updated on 24-Mar-2025 15:21:04

24 Views

In this article, we will discuss how to create a C++ program to generate the mirror image of a matrix across its diagonal. The diagonal mirror of a matrix means that we swap the elements at symmetric positions with respect to the diagonal. Let's break down the problem step by step and then look at the code that solves this problem. Understanding the Problem For any given matrix, the mirror image across the diagonal means that the element at position (i, j) will be swapped with the element at position (j, i). This operation is also known as transposing ... Read More

Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array

Nishu Kumari
Updated on 24-Mar-2025 15:22:24

29 Views

In this article, we will learn how to count pairs of numbers from a given array where the bitwise AND of the two numbers is greater than the bitwise XOR. Bitwise operations work on the individual bits of numbers. The AND operation results in 1 only when both bits are 1, while XOR gives 1 when the bits are different. Given an array of integers, we need to count how many pairs (i, j) exist such that the bitwise AND of the pair is greater than the bitwise XOR. In other words, for each pair of elements in the ... Read More

Count maximum number of disjoint pairs having one element not less than k times the other in C++

Nishu Kumari
Updated on 24-Mar-2025 15:23:03

38 Views

In this article, we will discuss how to count the maximum number of disjoint pairs in an array, where for each pair (x, y), one element is not less than K times the other. The goal is to find the number of such valid pairs in the array using C++. Given an array of integers, the task is to find all pairs (i, j) such that: arr[i] > K * arr[j] or arr[j] > K * arr[i] holds true for a given constant K. The condition is that no element ... Read More

Sorting strings with decimal points in C++

AYUSH MISHRA
Updated on 21-Mar-2025 19:01:25

267 Views

A string containing decimal points is sorted to solve complex programming problems. This question was asked in the Accenture coding assessment in 2025. In C++, there are different ways to sort such strings efficiently. In this article, we are going to discuss various approaches to sorting strings that contain decimal points using C++. How to Sort Strings with Decimal Points? In this problem, we are given an array of strings, where each string represents a decimal number. The goal is to sort these strings in increasing numerical order while ensuring the correct handling of decimal values. Example 1 ... Read More

Sorting an array of binary values - C++

AYUSH MISHRA
Updated on 21-Mar-2025 19:00:54

262 Views

Sorting an array of binary values (0s and 1s) is a common problem in programming. In C++, there are multiple ways to efficiently sort an array containing only 0s and 1s. In this article, we are going to learn various approaches to sorting an array of binary values using C++. How to Sort an Array of Binary Values? In this problem, we are given an array that consists of only 0s and 1s. The task is to sort the array in ascending order, which means all 0s should come before all 1s. Example 1 ... Read More

Advertisements