Server Side Programming Articles

Page 1452 of 2109

Check if a number can be represented as a sum of 2 triangular numbers in C++

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

In this section, we will see if we can express one number as the sum of two triangular numbers or not. The triangular numbers are like below −From the example, we can see that 1, 3, 6, 10 are some triangular numbers. We need to express a number N (say 16) as sum of two triangular numbers (6, 10).The approach is very simple. We have to get all triangular numbers less than N. Form a set from these values. Now we have to take a number say X from the set, and check whether N – X is present in ...

Read More

Check if a number can be written as sum of three consecutive integers in C++

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

In this section we will see, whether a number can be represented as tree consecutive numbers or not. Suppose a number is 27. This can be represented as 8 + 9 + 10.This can be solved in two different approach. The first approach is Naïve approach. In that approach, we have to check i + (i + 1) + (i + 2) is same as number or not. Another efficient approach is by checking whether the number is divisible by 3 or not. Suppose a number x can be represented by three consecutive 1s, then x = (y - 1) ...

Read More

Check if a number has same number of set and unset bits in C++

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

In this section we will check whether a number has same number of set bits and unset bits or not. Suppose the number 12 is there. Its binary representation is 1100. This has same number of 0s and 1s.The approach is simple. We will check each bit of the number, and if that is 1, then increase set_bit_count, and if it is 0, then increase unset_bit_count. Finally, if they are same, then return true, otherwise false.Example#include using namespace std; bool hasSameSetUnset(int n) {    int set_count = 0, unset_count = 0;    while(n){       if((n & 1) ...

Read More

Check if a number is a power of another number in C++

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

Here we will see, whether a number is power of another number or not. Suppose a number 125, and another number 5 is given. So it will return true when it finds that 125 is power of 5. In this case it is true. 125 = 53.AlgorithmisRepresentPower(x, y): Begin    if x = 1, then       if y = 1, return true, otherwise false    pow := 1    while pow < y, do       pow := pow * x    done    if pow = y, then       return true    return false ...

Read More

Check if a number is Full Prime in C++

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

Here we will see, how to check, whether a number is full prime or not. A number is said to be a full prime, if it is prime, and all of its digits are also prime. Suppose a number is 37, this is full prime. But 97 is not full prime as 9 is not a prime number.One efficient approach is that; first we have to check whether any digit is present that is not prime. Digits must be in 0 to 9. In that range 2, 3, 5 and 7 are prime, others are not prime. If all are ...

Read More

C++ program to find union and intersection of two unsorted arrays

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 2K+ Views

In this article, we will be discussing a program to find the union and intersection of two given unsorted arrays.Let us denote the two arrays with ‘A’ and ‘B’. Then union of those arrays is denoted by A ∪ B which is basically an array of all the elements in both the given arrays; provided that each element repeats only once.To find this, we will create a separate array and copy down all the elements from the first array. Then we will traverse through the elements of the second array and check if it is already present in the union ...

Read More

C++ program to find unique pairs such that each element is less than or equal to N

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

In this article, we will be discussing a program to find unique pairs of numbers having elements less than or equal to N and following some certain conditions −The square of difference between the two numbers must be equal to the LCM of those two numbers.The HCF of those two numbers can be represented as a product of any two consecutive numbers.The best approach to solve this problem would be to take two consecutive numbers (starting from 1) and finding the multiples of product of those numbers. Then among the multiples, to specify to one pair of numbers we need ...

Read More

C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers

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

In this article, we will be discussing a program to find ways an integer (say X) can be expressed as sum of n-th power of unique natural numbers.For example, let X = 100 and n = 2Then there would be 3 ways to express 100 as sum of squares of natural numbers.100 = 102 100 = 62 + 82 100 = 12 + 32 + 42 + 52 + 72This can be done easily by using recursion. We would start from 1 and go till the n-th root of the given number. In every run, we would subtract the n-th ...

Read More

C++ program for Finite Automata algorithm for Pattern Searching

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 1K+ Views

In this article, we will be discussing a program to execute the Finite Automata algorithm for pattern searching.We are provided with a text[0...n-1] and a pattern[0...m-1]. We have to find all the occurrences of the pattern[] in the text[].For this we would preprocess the text[] and build a 2-d array to represent it. After that we just have to traverse between the elements of the text[] and the different states of the automata.Example#include #include #define total_chars 256 int calc_nextstate(char *pat, int M, int state, int x) {    if (state < M && x == pat[state])       return ...

Read More

C++ program to find 'k' such that its modulus with each array element is same

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

In this article, we will be discussing a program to find an integer ‘k’, such that its modulus with each element of a given array is the same.For example, let us suppose we have been given with an array, arr = {12, 22, 32}Then we have the output value of k = 1, 2, 5, 10.Take the case of two values in the array ‘x’ and ‘y’ (x>y). Then we have (y+difference)%k = y%k. Solving this we get, difference%k = 0So, we will find all the divisors to the difference of the maximum and minimum element in the array and ...

Read More
Showing 14511–14520 of 21,090 articles
Advertisements