Category Archives: Bit Magic

Length of longest subsequence whose XOR value is odd

Given an array arr[] of N positive integers, the task is to find the length of the longest subsequence such that Bitwise XOR of all… Read More »

Count pair of strings whose concatenation has every vowel

Given an array arr[] of N strings. The task is to find the count of all possible pairs of strings such that their concatenations has… Read More »

Minimum integer with at most K bits set such that their bitwise AND with N is maximum

Given an integer N which can be represented in 32 bit, the task is to find another integer X that has at most K bits… Read More »

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.Examples: … 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 bit… 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: arr[]… 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 »