Category Archives: Bit Magic

Check divisibility in a binary stream

Stream of binary number is coming, the task is to tell the number formed so far is divisible by a given number n. At any given time, you will get 0 or 1 and tell whether the number formed with these bits is divisible by n or not. Generally, e-commerce companies ask this type of… Read More »

Multiplication of two numbers with shift operator

For any given two numbers n and m, you have to find n*m without using any multiplication operator. Examples: Input: n = 25 , m = 13 Output: 325 Input: n = 50 , m = 16 Output: 800 We can solve this problem with the shift operator. The idea is based on the fact… Read More »

Check if a number has bits in alternate pattern

Given an integer n > 0, the task is to find whether this integer has an alternate pattern in its bits representation. For example- 5 has an alternate pattern i.e. 101. Print “Yes” if it has an alternate pattern otherwise “No”. Here alternate pattern can be like 0101 or 1010. Examples: Input : 15 Output… Read More »

Subset sum queries using bitset

Given an array arr[] and a number of queries, where in each query we have to check whether a subset whose sum is equal to given number exists in the array or not. Examples: Input : arr[] = {1, 2, 3}; query[] = {5, 3, 8} Output : Yes, Yes, No There is a subset… Read More »