# Category Archives: Bit Magic

## Find Kth smallest value for b such that a + b = a | b

Given a number a and k, the task is to find the k’th smallest value for b such that a + b = a |… Read More »

## Construct XOR tree by Given leaf nodes of Perfect Binary Tree

Given the leaf nodes of a perfect binary tree, the task is to construct the XOR tree and print the root node of this tree.… Read More »

## Generating N-bit Grey Code starting from K

Given a value N and K, the task is to generate N-bits Gray Code starting from the value K. Examples: Input: N = 2, K… Read More »

## Check if all bits can be made same by flipping two consecutive bits

Given a binary string, the task is to find whether all the digits of the string can be made equal i.e either 0 or 1… Read More »

## Maximize distance between any two consecutive 1’s after flipping M 0’s

Given the size of a binary array consisting of 0’s only as n and an integer m which is the number of flips allowed from… Read More »

## Find the sum of power of bit count raised to the power B

Given an integer, array A. Find the sum of set bits raised to the power A[i] for each element in A[i]. Example: Input: N =… Read More »

## Maximum XOR value of maximum and second maximum element among all possible subarrays

Given an array arr[] of N distinct positive integers, let’s denote max(i, j) and secondMax(i, j) as the maximum and the second maximum element of… Read More »

## Append two elements to make the array satisfy the given condition

Given an array arr[] of non-negative integers, let’s define X as the XOR of all the array elements and S as the sum of all… Read More »

## Find the number obtained after concatenation of binary representation of M and N

Given two integers M and N the task is to find the number formed by concatenating the binary equivalents of M and N i.e. M… Read More »

## Invert the Kth most significant bit of N

Given two non-negative integers N and K, the task is to invert the Kth most significant bit of N and print the number obtained after… Read More »

## Maximize the number by flipping at most K bits

Given an integer N, the task is to find the greatest number that can be obtained by flipping at most K bits in the binary… Read More »

## Minimum count of Full Binary Trees such that the count of leaves is N

Given an integer N and an infinite number of Full Binary Trees of different depths, the task is to choose minimum number of trees such… Read More »

## Winner in the Rock-Paper-Scissor game using Bit manipulation

Two players are playing a series of games of Rock–paper–scissors. There are a total of N games played represented by an array arr[][] where arr[i]… Read More »

## Find the value of N XOR’ed to itself K times

Given two integer N and K, the task is to find the value of N XOR N XOR N XOR … XOR N (K times).… Read More »

## Find the minimum spanning tree with alternating colored edges

Given a graph with N nodes and M edges where each edge has a color (either black or green) and a cost associated with it.… Read More »