Articles on Trending Technologies

Technical articles with clear explanations and examples

Find the first natural number whose factorial is divisible by x in C++

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

We have to find the first natural number whose factorial is divisible by x. The x is given by the user. So if the x = 16, then output will be 6. as 6! mod 16 = 0. We will use general approach to solve this problem. iteratively count 1!, 2!, …. n! and check divisibility using x. If modulus is 0, then stop and return the number.Example#include using namespace std; int getNumber(int x) {    int fact = 1;    int i = 0;    while(fact % x != 0){       i++;       fact = fact * i;    }    return i; } int main() {    int x = 16;    cout

Read More

Find the kth node in vertical order traversal of a Binary Tree in C++

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

Suppose we have a binary tree and a value K. The task is to print the Kth node in the vertical order traversal. If no such node exists, then return -1. So if the tree is like below −The vertical order traversal is like −4 2 1 5 6 3 8 7 9So if K = 3, then result will be 1.The approach is simple. We will perform the vertical order traversal, then check the current node is the kth node or not, if so then return.Example#include #include #include #include using namespace std; class Node {    public:    int ...

Read More

Find a number x such that sum of x and its digits is equal to given n using C++.

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

Here we will see one problem, where we take a number n, we have to find another value say x, such that x + digit sum of x is same as the given number n. Suppose the value of n is 21. This program will return a number x = 15, as 15 + digit sum of 15, i.e. 15 + 1 + 5 = 21 = n.To solve this problem, we have to follow simple approach. We will iterate through 1 to n, in each iteration, we will see if the sum of the number and its digit sum ...

Read More

Find the largest interval that contains exactly one of the given N integers In C++

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

Suppose we have an array of N distinct integers. We have to find the max element in an interval [L, R] such that the interval contains exactly one of the given N integers and 1

Read More

Find an array element such that all elements are divisible by it using c++

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

Consider we have an array A with few elements. We have to find an element from A, such that all elements can be divided by it. Suppose the A is like [15, 21, 69, 33, 3, 72, 81], then the element will be 3, as all numbers can be divisible by 3.To solve this problem, we will take the smallest number in A, then check whether all numbers can be divided by the smallest number or not, if yes, then return the number, otherwise, return false.Example#include #include using namespace std; int getNumber(int a[], int n) {    int minNumber = ...

Read More

Find the largest number that can be formed with the given digits in C++

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

Suppose we have an array of digits. We have to find the maximum number that can be obtained using all digits of the array. So if the array is like [3, 3, 9, 6, 2, 5], then maximum number can be 965332.From the problem, we can see that we can easily sort the digits in non-increasing order, then print them. But we can solve this using more efficient way. We can create one array of size 10 to store the frequency of each digit, then print the numbers from 9 to 0 accordingly.Example#include #include using namespace std; int ...

Read More

Find an element in array such that sum of left array is equal to sum of right array using c++

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

Suppose we have an array A, it has n elements. Our task is to divide the array A into two subarrays, such that the sum of each subarray will be the same. Suppose the array A = [2, 3, 4, 1, 4, 5], The output is 1, so subarrays before 1 and after 1 are taken. [2, 3, 4], and [4, 5].To solve this problem, we will calculate the whole array except for the first element in right_sum. Consider that is the partitioning element. We will traverse from left to right. Subtracting an element from right_sum and adding an element ...

Read More

Find nth number that contains the digit k or divisible by k in C++

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

Given two positive integers n and k, and we have to find the nth number that contains the digit k or divisible by k. The k will be in range [2 to 9]. So if n and k are 15 and 3 respectively, then output is 33. As the numbers [3, 6, 9, 12, 13, 15, 18, 21, 23, 24, 27, 30, 31, 33] These are those numbers where each element contains the digit k = 3 or divisibility by k and in this nth number is 33. So output is 33.Check each number that contains k and multiple of ...

Read More

Find and print duplicate words in std::vector using STL functions using C++.

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

Consider we have a list of strings. The list has some duplicate strings. We have to check which strings are occurred more than once. Suppose the string list is like [“Hello”, “Kite”, “Hello”, “C++”, “Tom”, “C++”]Here we will use the hashing technique, so create an empty hash table, then traverse each string, and for each string, s is already present in the hash, then display the string, otherwise insert into the hash.Example#include #include #include using namespace std; void displayDupliateStrings(vector strings) {    unordered_set s;    bool hasDuplicate = false;    for (int i = 0; i

Read More

Find number from given list for which value of the function is closest to A in C++

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

Suppose we have a function F(n) such that F(n) = P – (0.006*n), where P is also given. Given a list of integers and a number A. The task is to find the number from given list, for which the value of the function is nearer to A. So if P = 12, and A = 5, then list will be {1000, 2000} So output will be 1000. So if P = 12 and A = 5, then for 1000, F(1000) = 12 – (0.006 * 1000) = 6 and for 2000, F(2000) = 12 – (0.006 * 2000) = ...

Read More
Showing 28501–28510 of 61,297 articles
Advertisements