www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Hamming Distance

Last update: 2/2/2017 2:26:00 AM

By Fatih KABAKCI

The problem is to figure out how many corresponding bits are different of given x and y. This is known as humming distance. Humming distance compares each corresponding bits of two value by looking if they are different. If so, then adds 1 to the value of humming distance.

Problem #461 - Hamming Distance

Description: The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.


Note:
0 ? x, y < 2^31.

Example:

Input: x = 1, y = 4

Output: 2

Explanation:
1   (0 0 0 1)
4   (0 1 0 0)
       d   d

The above arrows point to positions where the corresponding bits are different.

Difficulty: Easy

Solution in Java

Shorter way

As known, XOR operation is a calculation that sets result to 1 if two corresponding bits are different. Thus, we can reach to the result directly by using XOR operation in Java. Then, what we need to do is basically counting number of 1 bits that gets us to the final point which is a humming distance.

public class Solution {
    public int hammingDistance(int x, int y) {
		int xORy = x ^ y;
        return Integer.bitCount(xORy);
    }
}

Long way

package com.fatihkabakci.Easy.HammingDistance;

/**
 * @author fkabakci
 * Problem Description: The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
 * 
 * Given two integers x and y, calculate the Hamming distance.
 * 
 * Solution:
 * 1. Find max and min.
 * 2. Get length of bits of the greater one.
 * 3. Get bits of both with the capacity of the greater one.
 * 4. Figure out the difference of bit positions.
 * 
 * Example:
 * 
 * Input: x = 1, y = 4
 * 
 * Output: 2
 * 
 * Explanation:
 * 1   (0 0 0 1)
 * 4   (0 1 0 0)
 *  	  d   d
 * Total: 2 
 * 
 * The above arrows point to positions where the corresponding bits are different.
 */

public class Solution {
	private int getBitLength(int number) {
		int i = 0;
		for (i = 0; i < 31 && number >= Math.pow(2, i); i++);
		return i;
	}

	private byte[] toBits(int number, int bitLength) {
		int capacity = getBitLength(number);
		if (bitLength < capacity) {
			bitLength = capacity;
			System.err.println("Attempt to override capacity !");
		}

		byte[] bits = new byte[bitLength];
		for (int i = bitLength; number > 0; i--) {
			bits[i - 1] = (byte) (number % 2);
			number = number / 2;
		}
		return bits;
	}

	public int hammingDistance(int x, int y) {
		int max = Math.max(x, y);
		int min = Math.min(x, y);

		int maxBitLength = getBitLength(max);

		byte[] bitsX = toBits(max, maxBitLength);
		byte[] bitsY = toBits(min, maxBitLength);

		int hammingDistance = 0;
		for (int i = 0; i < maxBitLength; i++)
			if (bitsX[i] != bitsY[i])
				hammingDistance++;

		return hammingDistance;
	}

	public static void main(String[] args) {
		int hammingDistance = new Solution().hammingDistance(1, 2);
		System.out.println(hammingDistance);
	}
}

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.