Given an integer, reverse its bits using binary operators. The idea is to initialize the result by 0 (all bits 0) and process the given number starting from its least significant bit. If the current bit is 1, then we set the corresponding most significant bit in the result and finally move on …
Given an array having elements between 0 to 31, find elements which occurs odd number of times without using the extra space.
Huffman Coding (also known as Huffman Encoding) is a algorithm for doing data compression and it forms the basic idea behind file compression. This post talks about fixed length and variable length encoding, uniquely decodable codes, prefix rules and construction of Huffman Tree.
Given an array of integers with all its elements between 1 to n with the exception of two elements which occur twice. Find two duplicate elements without using any extra memory.
Given a positive number N, efficiently generate binary numbers between 1 to N using queue data structure and in linear time.
Given an limited range array of integers of size n with all its elements between 1 to n with the exception of one element which occur twice and one number which is missing from the list. Find missing number and the duplicate element in linear time and without using any extra memory. For example, …
Given an integer, swap consecutive b bits starting from given positions in binary representation of the integer. The bits to be swapped should not overlap with each other.
Given a number n, find previous power of 2. If n itself is a power of 2, return n.
Given a number n, find next highest power of 2. If n itself is a power of 2, return n.
Given two 16-bit positive values stored in 32-bit integer variables, find the product using the 8-bit multiply operator that that takes two 8-bit numbers and returns a 16-bit value.
Given two integers, find their minimum and maximum without using branching.
Given an integer, count set bits in it.