Programming Articles

Page 1459 of 2547

Find minimum number to be divided to make a number a perfect square in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have a number N. We have to find minimum number that divide N to make it perfect square. So if N = 50, then minimum number is 2, as 50 / 2 = 25, and 25 is a perfect square.A number is perfect square if it has even number of different factors. So we will try to find the prime factors of N, and find each prime factor power. Find and multiply all prime factors whose power is odd.Example#include #include using namespace std; int findMinimumNumberToDivide(int n) {    int prime_factor_count = 0, min_divisor = 1;    while (n%2 ...

Read More

Find minimum radius such that atleast k point lie inside the circle in C++

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

Suppose we have some points, and one integer k. We have to find minimum radius of a circle whose center is at (0, 0) to cover k points. So if the points are like (1, 1), (-1, -1), (1, -1), and k = 3, then radius will be 2.Here we will find the Euclidean distance between each point and (0, 0), then sort the distances and return the kth element after sorting.Example#include #include using namespace std; struct point{    int x, y; }; int minRadius(int k, point points[], int n) {    int dist[n];    for (int i = 0; ...

Read More

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 206 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 265 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 451 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 379 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 674 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
Showing 14581–14590 of 25,466 articles
Advertisements