www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Number Complement

Last update: 2/6/2017 2:40:00 AM

By Fatih KABAKCI

The problem is to find out complement of a number.

Problem #476 - Number Complement

Description: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:
1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
2. You could assume no leading zero bit in the integer’s binary representation.


Example 1:
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:
Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.NumberComplement;

/**
 * @author fkabakci
 * Problem Description: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
 * 
 * Note:
 * 		1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
 * 		2. You could assume no leading zero bit in the integer’s binary representation.
 * 
 * Example 1:
 * Input: 5
 * Output: 2
 * 
 * Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
 * 
 * Example 2:
 * Input: 1
 * Output: 0
 * 
 * Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
 * 
 * Solution:
 * 1. Find out the bits.
 * 2. Figure out powers of 2 times complement value and adds result.
 */

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) {
		return toBits(number, getBitLength(number));
	}

	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 findComplement(int num) {
		byte[] bits = toBits(num);
		int l = bits.length;
		int complement = 0;
		for (int i = 0; i < l; i++) {
			byte bit = (byte) ((bits[i] == 0) ? 1 : 0);
			complement += bit * Math.pow(2, l - 1 - i);
		}
		return complement;
	}

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

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,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.