C++ Articles

Page 38 of 597

Find all divisors of a natural number - Set 2 in C++

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

In this tutorial, we are going to write a program that finds all the divisors of a natural number. It's a straightforward problem. Let's see the steps to solve it.Initialize the number.Write a loop that iterates from 1 to the square root of the given number.Check whether the given number is divisible by the current number or not.If the above condition satisfies, then print the current number and given_number/current_number.ExampleLet's see the code.#include using namespace std; void findDivisors(int n) {    for (int i = 1; i

Read More

Find amount of water wasted after filling the tank in C++

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

In this tutorial, we are going to solve the following problem.Given a tank with a capacity of N liters and a pump that fill the tank with S speed per minute. Unfortunately, there is a hole in the tank. And water is wasting at a speed of WS per minute while filling it.We need to calculate the amount of water wasted for a full tank.The amount of water filled per minute is equal to the difference between the water filling water and wasting water speed.Hence we can get the total time to fill the water tank by dividing the capacity ...

Read More

Find any one of the multiple repeating elements in read only array in C++

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

In this tutorial, we are going to write a program that finds the repeating element in the given array.Let's see the steps to solve the problem.Initialize the array.Initialize a counter map to store the frequency of each element in the array.Iterate over the array.Count each element.Print the element whose frequency is greater than 1.ExampleLet's see the code.#include using namespace std; int findRepeatingElement(int arr[], int n) {    map frequencies;    for (int i = 0; i < n; i++) {       map::iterator itr = frequencies.find(arr[i]);       if (itr != frequencies.end()) {         ...

Read More

Birthday Paradox in C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 825 Views

The birthday paradox is a very famous problem in the section of probability. The problem statement of this problem is stated as, There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate no. of people at a birthday party on the basis of having the same birthday.In the probability we know that the chance of getting ahead is 1/2 same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept, The chance of two people having the different birthday ...

Read More

Counting elements in two arrays using C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 833 Views

Let us assume that we have given two unsorted arrays arr1[] and arr2[]. The task is to count the total number of elements in arr2[] for which each element of arr1[] are less than or equal to the elements present in arr2[]. However, the element in both the array may contain duplicates too.For example, Input-1 −N = 6 M = 7 arr1[N] = {1, 2, 5, 0, 6, 3} arr2[M] = {0, 0, 1, 2, 1, 3, 4, 6, 8}Output −4 5 7 2 8 6The approach used to solve this problemTo count every element of arr1[] and check if ...

Read More

Find length of the largest region in Boolean Matrix in C++

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

In this problem, we are given a 2-D matrix of size nXm consisting of 0’s and 1’s only. Our task is to find the length of the largest region in the Boolean Matrix. Problem Description: If a cell contains 1, it is a filled Cell. We need to find the length of connected cells which are connected adjacent to each other horizontally or vertically or diagonally. Let’s take an example to understand the problem,  Input: matrix[4][5]{ {0, 1, 1, 0, 1},    {0, 0, 1, 1, 1},    {1, 0, 0, 0, 0},    {1, 0, 1, 0, 1} }Output: 6Explanation:  The number of connected filled ...

Read More

Find m-th smallest value in k sorted arrays in C++

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

In this problem, we are given k different arrays of different sizes. Our task is to find m-th smallest value in k sorted arrays. Problem Description: Here, we need to find m-th smallest element of the merged array of all k arrays.Let’s take an example to understand the problem,  Input:         m = 4                   arr[][] = { {4 , 7},                                     {2, 5, 6},                       ...

Read More

Print cells with same rectangular sums in a matrix in C++

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

In this problem, we are given a matrix mat of size mXn of integer values. Our task is to create a program to Print cells with same rectangular sums in a matrix.Problem description: We will be finding a cell in the matrix in such a way that the sum of sub-matrices that are starting and ending with the cell is equal to the sum of all the remaining elements. For a cell the sum of matrix (a, b) sub-matrix mat[0][0] to mat[a][b] and mat[a][b] to mat[m][n] is equal to the sum of all remaining elements.Let’s take an example to understand the problem, ...

Read More

Write a program in C++ to replace all the 0's with 5 in a given number

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 2K+ Views

Given an integer N, the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example, Input-1 −N= 1007Output −1557Explanation − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.Input-2 −N = 00105Output −155Explanation − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.Approach to Solve this ProblemTo replace all the ...

Read More

Print BST keys in given Range - O(1) Space in C++

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

In this problem, we are given two values k1 and k2 (k1 < k2), and the root of the binary search tree. Our task is to create a program to Print BST keys in given Range. Problem Description: We will print all the keys of the tree from n1 to n2 in increasing order.Let’s take an example to understand the problem,  Input: k1 = 4, k2 = 12Output: 6, 7, 9Solution ApproachSimple we can solve the problem using inorder traversal but the space complexity there is 0(n) but the need of the hour is to solve in O(1) complexity. So, for this, we will use a ...

Read More
Showing 371–380 of 5,962 articles
« Prev 1 36 37 38 39 40 597 Next »
Advertisements