# Category Archives: Bit Magic

## Queries for count of array elements with values in given range with updates

Given an array arr[] of size N and a matrix Q consisting of queries of the following two types: 1 L R : Print the… Read More »

## Sum of first N natural numbers with all powers of 2 added twice

Given an integer N, the task is to calculate the sum of first N natural numbers adding all powers of 2 twice to the sum.… Read More »

## Sum of Hamming difference of consecutive numbers from 0 to N | Set 2

Given a number N, the task is to find the sum of Hamming difference of consecutive numbers from 0 to N. Hamming Distance between two… Read More »

## First number to leave an odd remainder after repetitive division by 2

Given two integers A and B, the task is to print the integer among the two, which will be converted to an odd integer by… Read More »

## Construct the smallest possible Array with given Sum and XOR

Given two positive integers S and X which represents the sum and Bitwise XOR of all the elements of an array arr[]. The task is… Read More »

## XOR of a subarray (range of elements) | Set 2

Given an array integer arr[] of size N and Q queries. Each query is of the form (L, R), where L and R are indices… Read More »

## Maximum bitwise OR value of subsequence of length K

Given an array arr[] of N positive integers and a number K, the task is to find the maximum value of bitwise OR of the… Read More »

## Largest number M less than N such that XOR of M and N is even

Given a positive integer N, the task is to find the largest integer M such that 0 <= M < N and XOR(M, N) is… Read More »

## Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart

Given a binary string s and a number K, the task is to find the maximum number of 0s that can be replaced by 1s… Read More »

## Sum of consecutive bit differences of first N non-negative integers

Given a positive integer N, the task is to find out the sum of all consecutive bit differences from 0 to N. Note: If the… Read More »

## Generate an Array such with elements maximized through swapping bits

Given an array arr[], the task is to generate a modified array such that all its elements are maximized by swapping of bits. Examples: Input:… Read More »

## Print all the leaf nodes of Binary Heap

Given an array of N elements which denotes the array representation of binary heap, the task is to find the leaf nodes of this binary… Read More »

## Minimize K whose XOR with given array elements leaves array unchanged

Given an array of N elements, the task is to find the minimum value of K such that Bitwise XOR of K with all array… Read More »

## Count of elements which cannot form any pair whose sum is power of 2

Given an array arr[] of length N, the task is to print the number of array elements which cannot form a pair with any other… Read More »

## Maximum OR value of a pair in an Array | Set 2

Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.… Read More »