Server Side Programming Articles - Page 2148 of 2650

Check if a number is formed by Concatenation of 1, 14 or 144 only in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:27:08

238 Views

Here we will see one problem, that can tell that whether a string or a number is a concatenation of 1, 14 or 144 only. Suppose a string is “111411441”, this is valid, but “144414” is not valid.The task is simple, we have to fetch a single digit, double-digit and triple-digit number from the last, and check whether they match with any of these three (1, 14 and 144), if we get one match, divide the number with it, and repeat this process until the entire number is not exhausted.Example#include #include using namespace std; bool checkNumber(long long number) ... Read More

Check if a number is divisible by 41 or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:22:18

198 Views

Here we will see one program, that can check whether a number is divisible by 41 or not. Suppose a number 104413920565933 is given. This is divisible by 41.To check the divisibility, we have to follow this rule −Extract the last digit of the number/truncated number every timesubtract 4 * (last digit of the number calculated previous) to the truncated numberRepeat these steps as long as necessary.30873, so 3087 - 4*3 = 3075 3075, so 307 - 4 * 5 = 287 287, so 28 – 4 * 7 = 0 So, 30873 is divisible by 41.Example Live Demo#include #include ... Read More

Check if a number is divisible by 23 or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:19:20

223 Views

Here we will see one program, that can check whether a number is divisible by 23 or not. Suppose a number 1191216 is given. This is divisible by 23.To check the divisibility, we have to follow this rule −Extract the last digit of the number/truncated number every timeadd 7 * (last digit of the number calculated previous) to the truncated numberRepeat these steps as long as necessary.17043, so 1704 + 7*3 = 1725 1725, so 172 + 7 * 5 = 207 207, this is 9 * 23, so 17043 is divisible by 23.Example Live Demo#include #include using namespace ... Read More

Check if a number is a Krishnamurthy Number or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:16:33

2K+ Views

Here we will see how to check a number is Krishnamurty number or not. A number is Krishnamurty number, if the sum of the factorial of each digit is the same as the number. For example, if a number is 145, then sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. So this is a Krishnamurty number, The logic is simple, we have to find the factorial of each number, and find the sum, then if that is the same as a given number, the number is Krishnamurty number. Let us see the code ... Read More

Check if a number has bits in alternate pattern - Set-2 O(1) Approach in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:12:32

474 Views

Let us consider we have an integer n. The problem is to check, whether this integer has alternate patterns in its binary equivalent or not. The alternate pattern means 101010….The approach is like: calculate num = n XOR (n >> 1), now if all bits of num is 1, then the num has alternating patterns.Example Live Demo#include #include using namespace std; bool isAllBitSet(int n){    if (((n + 1) & n) == 0)       return true;    return false; } bool hasAlternatePattern(unsigned int n) {    unsigned int num = n ^ (n >> 1);    return isAllBitSet(num); } int main() {    unsigned int number = 42;    if(hasAlternatePattern(number))       cout

Check if a number has bits in alternate pattern - Set 1 in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:08:52

292 Views

Let us consider we have an integer n. The problem is to check, whether this integer has alternate patterns in its binary equivalent or not. The alternate pattern means 101010….The approach is like: check each digit using binary equivalent, and if two consecutive are same, return false, otherwise true.Example#include using namespace std; bool hasAlternatePattern(unsigned int n) {    int previous = n % 2;    n = n/2;    while (n > 0) {       int current = n % 2;       if (current == previous) // If current bit is same as previous       return false;       previous = current;       n = n / 2;    }    return true; } int main() {    unsigned int number = 42;    if(hasAlternatePattern(number))       cout

Check if a number can be expressed as sum two abundant numbers in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:07:23

170 Views

Suppose we have a number. We have to express this as sum of two Abundant number, if yes, print the numbers, otherwise print -1. A number is said to be Abundant number is sum of all proper divisor of the number, denoted by sum(n) is greater than the value of number.To solve this, we will store all abundant numbers into a set, and for given number n, run a loop for i = 1 to n, and check n and (n – i) are abundant or not.Example#include #include #define N 100005 using namespace std; set getAbundantSet() {   ... Read More

Check if a given string is a rotation of a palindrome in C++

Arnab Chakraborty
Updated on 21-Oct-2019 09:15:13

338 Views

Here we will see, one string is palindrome after certain rotation or not. A palindrome is a string that is the same in both directions. A string rotation is a palindrome if that is like AAAAD. this is not a palindrome directly, but its rotation AADAA is a palindrome.To check a string is rotated palindrome or not, then we will check this is a palindrome or not at the first time, after that, rotate it by one character, then check again, this checking will be performed n amount of time, where n is the number of characters.Example#include #include ... Read More

Check if a given number is sparse or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 09:12:24

580 Views

In this section, we will see how to check a number is sparse or not. A number is said to be sparse if the binary representation of the number, has no two or more than two consecutive 1s. Suppose a number is like 72. This is 01001000. Here no two or more consecutive 1s.To check a number is sparse or not, we will take the number as n, then shift that number one bit to the right, and perform bitwise AND. if the result is 0, then that is a sparse number, otherwise not.Example Live Demo#include using namespace std; bool ... Read More

Check if a given matrix is sparse or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 09:08:18

495 Views

Here we will see how to check whether a matrix is sparse or not. A sparse matrix is a matrix where most of the elements are 0. The definition of a sparse matrix is, if the 2/3rd of the elements are 0, then the matrix is denoted as a sparse matrix. Here is the example of a sparse matrix.To check it, we will count the number of 0s in the matrix, then if that count is greater than 2/3rd of the total elements, then this is sparse.Example Live Demo#include #include #define MAX 5 using namespace std; bool isSparseMatrix(int arr[][MAX], ... Read More

Advertisements