Found 7347 Articles for C++

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

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

213 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

100 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

216 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

392 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

375 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

Check if a given array is pairwise sorted or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 09:04:02

102 Views

We have an array A, with n elements. We have to check whether the array is pairwise sorted or not. Suppose the array is like {8, 10, 18, 20, 5, 15}. This is pairwise sorted as (8, 10), (18, 20), (5, 15) are sorted. If the array has an odd number of elements, then the last one will be ignored.The approach is too simple, by taking I from 0 to n-1, we will see if the ith element is less than the i+1th element or not, if not, then return false, otherwise increase I by 2.Example Live Demo#include #include ... Read More

Check if a circle lies inside another circle or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 09:01:17

500 Views

Suppose we have two circles (center points, and the radius values), we have to check one circle is fit inside another circle or not. There are three possible causes.The smaller circle lies completely inside the bigger one, without touching each other. In this case, the sum of the distance between the centers, and the smaller radius, is lesser than a bigger radius. So the smaller one will be inside the bigger one.The second case is the smaller circle is inside the bigger ones, but also touches the circumference of the bigger circle.The third case is, some part of the smaller ... Read More

Check if a binary string contains consecutive same or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:58:08

239 Views

Suppose we have a binary string. Our task is to check whether the string has consecutive same characters or not. If there are consecutive same characters, then that is invalid, otherwise valid. Then the string “101010” is valid, but “10111010” is invalid.To solve this problem, we will traverse from left to right, if two consecutive characters are the same, then return false, otherwise true.Example Live Demo#include #include using namespace std; bool isConsecutiveSame(string str){    int len = str.length();    for(int i = 0; i

Check if a + b = c is valid after removing all zeroes from a, b and c in C++

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

57 Views

Suppose we have three numbers a, b, c, we have to check whether a + b = c, after removing all 0s from the numbers or not. Suppose the numbers are a = 102, b = 130, c = 2005, then after removing 0s, the numbers will be a + b = c : (12 + 13 = 25) this is trueWe will remove all 0s from a number, then we will check after removing 0s, a + b = c or not.Example Live Demo#include #include using namespace std; int deleteZeros(int n) {    int res = 0;   ... Read More

Check if a number is sandwiched between primes in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:49:00

64 Views

Here we will see whether a number is sandwiched between primes or not. A number is said to be sandwiched between primes when the number just after it, and just below it is prime numbers. To solve this, check whether n-1 and n+1 are prime or not.Example Live Demo#include #include #define N 100005 using namespace std; bool isPrime(int n) {    if (n == 0 || n == 1)       return false;    for (int i=2;i

Advertisements