C++ Articles

Page 3 of 597

Bricks Falling When Hit in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 262 Views

Suppose we have a grid of binary values (0s and 1s) the 1s in a cell represent the bricks. A brick will not drop when that satisfies these conditions −Either brick is directly connected to the top of the gridor at least one of its adjacent (top, bottom, left, right) bricks will not drop.We will do some erasures sequentially. In each case we want to do the erasure at the location (i, j), the brick (if that is present) on that location will disappear, and then some other bricks may drop because of that erasure. We have to find the ...

Read More

Maximum Subarray Sum Excluding Certain Elements in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 196 Views

In this tutorial, we will be discussing a program to find maximum Subarray Sum Excluding Certain Elements.For this we will be provided with two arrays of size M and N. Our task is to find a sub-array in the first array such that no element inside the subarray is present inside the second array and the elements of subarray sum up to be maximum.Example#include using namespace std; //checking if element is present in second array bool isPresent(int B[], int m, int x) {    for (int i = 0; i < m; i++)    if (B[i] == x)   ...

Read More

Write a Program to Find the Maximum Depth or Height of a Tree in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 879 Views

In this problem, we are given a binary tree. Our task is to write a program to find the maximum depth or height of the given tree.Let’s take an example to understand the problem, The height of the tree is 3.To find the maximum height of a tree, we will check the heights of its left and right subtree and add one to the maximum of both. This is a recursive process that will continue this the last node is of the tree is found and one is added progressively to find the height of the sub-trees.Above example solved using ...

Read More

Write a program to reverse digits of a number in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 659 Views

A program to reverse digits of a number will interchange the position of the digits and reverse there order.Let’s suppose be a number abcde the reverse will be edcba.Let’s take an example to understand the problem, Inputn = 786521Output125687To reverse digits of the number, we will take each digit of the number from MSB(unit digit) and add it to reverse number variable, after this divide the original number by 10 and the reverse_number is multiplied by 10. This will be done until the number becomes 0.This repetitive process can be accomplished by two methods, iteration, and recursion, we will create ...

Read More

What are C++ Floating-Point Constants?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 11-Mar-2026 2K+ Views

Floating-point constants specify values that must have a fractional part.Floating-point constants have a "mantissa, " which specifies the value of the number, an "exponent, " which specifies the magnitude of the number, and an optional suffix that specifies the constant's type(double or float).The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example −24.25 12.00These values can also contain exponents. For example, 24.25e3 which is equivalent to 24250In C++ you can use the following code to create a floating point constant −Example#include ...

Read More

Check if a binary string contains all permutations of length k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 191 Views

Suppose we have a binary string, another integer k. We have to check the string is containing all permutations of binary of k bits. Suppose a string is like “11001”, and if the K = 2, then it must have all permutations of k bit numbers. (00, 01, 10, 11), the given string has all permutation. So this is valid string.by taking the binary string and the value of k, we have to check whether binary sequences are matched or not. The binary sequence is made of size k. So there will be 2k number of different binary permutations. We ...

Read More

k-Rough Number or k-Jagged Number in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 281 Views

In this tutorial, we are going to write a program that checks whether the given number is k-rough or k-jagged number or not.The number whose smallest prime factor is greater than or equal to the given k, it is called k-rough or k-jagged number.Let's see the steps to solve the problem.Initialise the numbers n and k.Find all the prime numbers that are factors of n and store them in a vector.Get the first element from the vector and compare it with k to check whether n is k-rough or k-jagged number or not.ExampleLet's see the code.#include using namespace std; ...

Read More

K-th digit in &lsquo;a&rsquo; raised to power &lsquo;b&rsquo; in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 383 Views

In this tutorial, we are going to write a program that finds the k-th digit from the right side in the number abIt's a straightforward problem. Let's see the steps to solve it.Initialise the numbers a, b, and k.Find the value of abusing pow method.Write a loop that iterates until power value is less than zero or count is less than k.Get the last digit from the power value.Increment the counter.Check whether k and counter are equal or not.Return the digit if they are equalReturn -1.ExampleLet's see the code.#include using namespace std; int getTheDigit(int a, int b, int k) ...

Read More

K-th Element of Two Sorted Arrays in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 251 Views

In this tutorial, we are going to write a program that finds the k-th element from thee merged array of two sorted arrays.Let's see the steps to solve the problem.Initialise the two sorted arrays.Initialise an empty array of size m + n.Merge the two arrays into the new array.Return the k - 1 element from the merged array.ExampleLet's see the code.#include using namespace std; int findKthElement(int arr_one[], int arr_two[], int m, int n, int k) {    int sorted_arr[m + n];    int i = 0, j = 0, index = 0;    while (i < m && j ...

Read More

K-th Greatest Element in a Max-Heap in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 1K+ Views

In this tutorial, we are going to write a program that finds the k-th largest element from the max-heap.We will use priority queue to solve the problem. Let's see the steps to complete the program.Initialise the max-heap with correct values.Create a priority queue and insert the root node of the max-heap.Write a loop that iterates k - 1 times.Pop the greatest element from the queue.Add the left and right nodes of the above node into the priority queue.The greatest element in priority queue is the k-th greatest element now.Return it.ExampleLet's see the code.#include using namespace std; struct Heap { ...

Read More
Showing 21–30 of 5,961 articles
« Prev 1 2 3 4 5 597 Next »
Advertisements